用ggplot绘制geom_tile的连续强度 [英] Plot the intensity of a continuous with geom_tile in ggplot

查看:1713
本文介绍了用ggplot绘制geom_tile的连续强度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在空间上绘制一个连续变量。我看到这个例子得到了我需要的结果:

  library(MASS)
library( ggplot2)
library(reshape2)

DB <-melt(volcano)
ggplot(DB,aes(x = Var1,y = Var2,fill = value))+ geom_tile()



<$ p $
url< - https://www.dropbox.com/s/4m5qk32wjgrjq40/dato.RDATA
source_data(url)

library(ggplot2)
ggplot(dato,aes(y = variable,x = y,fill = value))+ geom_tile()



太棒了。但是我的x和y距离空间点的公里距离(东部和北部)。我将它们在经纬度上进行了转换。但现在我的情节不起作用!

  ggplot(dato,aes(y = lat,x = long,fill = value) ))+ geom_tile()



我不明白为什么。无论如何绘制我的数据像点一样,结果是非常相似的:

  ggplot(dato,aes(y = lat,x = long, ()= geom_point()
ggplot(dato,aes(y = variable,x = y,fill = value))+ geom_point()


解决方案

您可以作弊一下,并使用正方形的 geom_point

  #devtools :: install_github(sjmgarnier / viridis)
库(viridis)
库(ggplot2)
库(ggthemes)
库(比例)
库(网格)
$ b $ gg < - ggplot(dato)
gg< - gg + geom_point(aes(x = long,y = lat,color = value),shape = 15,size = 5)
gg < - gg + coord_equal()
gg < - gg + scale_color_viridis(na.value =#FFFFFF00)
gg< - gg + theme_map()
gg< - gg + theme(legend.position =right)
gg



我没有投影经纬度对并使用 coord_equal 。你应该为正在映射的区域使用适当的投影。



现在你让我好奇米兰周围的热点地区: - )

  gmap<  -  get_map(location = c(9.051062,45.38804,9.277473,45.53438),
source =stamen,maptype =碳粉,crop = TRUE)
gg< - ggmap(gmap)
gg< - gg + geom_point(data = dato,aes(x = long,y = lat,color = value ),shape = 15,size = 5,alpha = 0.25)
gg <-gg + coord_map()
gg < - gg + scale_color_viridis(na.value =#FFFFFF00)
gg< - gg + theme_map()
gg< - gg + theme(legend.position =right)
gg


I'm trying to plot a continuous variable on space. I saw this example that gets the same result that I need:

library("MASS")
library("ggplot2")
library(reshape2) 

DB<-melt(volcano)
ggplot(DB, aes(x=Var1, y=Var2, fill=value)) +geom_tile()

So I did with my data:

library(repmis)
url<-"https://www.dropbox.com/s/4m5qk32wjgrjq40/dato.RDATA"
source_data(url)

library(ggplot2)
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_tile()

That's wonderful. But my "x" and "y" are kilometers distance (east and north) from a point in space. I transformed these in latitude and longitude. But now my plot doesn't work!

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_tile()

I don't understand why. Anyway plotting my data like points the result is very similar:

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_point()
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_point()

解决方案

You can cheat a bit and use geom_point with a square shape:

#devtools::install_github("sjmgarnier/viridis")
library(viridis)
library(ggplot2)
library(ggthemes)
library(scales)
library(grid)

gg <- ggplot(dato)
gg <- gg + geom_point(aes(x=long, y=lat, color=value), shape=15, size=5)
gg <- gg + coord_equal()
gg <- gg + scale_color_viridis(na.value="#FFFFFF00")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

I did not project the lat/long pairs and just used coord_equal. You should use a proper projection for the region being mapped.

And, now you have me curious as to what those hot spots are around Milan :-)

gmap <- get_map(location=c(9.051062, 45.38804, 9.277473, 45.53438),
                 source="stamen", maptype="toner", crop=TRUE)
gg <- ggmap(gmap)
gg <- gg + geom_point(data=dato, aes(x=long, y=lat, color=value), shape=15, size=5, alpha=0.25)
gg <- gg + coord_map()
gg <- gg + scale_color_viridis(na.value="#FFFFFF00")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

这篇关于用ggplot绘制geom_tile的连续强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆