使用ggmap中的geom_raster绘制热图

问题描述 投票:1回答:1

我有一个包含经度,纬度和强度变量(var1.pred)的数据框。

my data frame

我想在ggmap地图上绘制一个平滑的填充等高线图。我使用geom_tile绘图完成了它,但它看起来不平滑。我减少了瓷砖尺寸,但是图表占用了大量存储空间。我想要做的是使用geom_raster在地图对象上绘制一个漂亮干净的等高线图。使用下面的代码我用ggplot完成了:

raster <- ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))+
geom_raster(aes(fill = var1.pred), alpha = 0.5) +
scale_fill_gradient(low = "white", high = "blue")+
geom_contour(colour = "white", binwidth = 1) +
labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude') 
raster  

这将返回图:enter image description here

但是我无法弄清楚如何将它与ggmap对象结合起来。我尝试了各种各样的东西,比如:

ggmap(fr) + #fr is a map from get_map
ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))+
geom_raster(aes(fill = var1.pred), alpha = 0.5) +
scale_fill_gradient(low = "white", high = "blue")+
geom_contour(colour = "white", binwidth = 1) +
labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')

但我得到错误:

错误:不知道如何将o添加到绘图中

我知道这个问题与组合ggplot和ggmap对象有关,但我无法弄清楚如何让它工作。任何帮助,将不胜感激。

谢谢,

知更鸟

r ggplot2 gis ggmap geom-raster
1个回答
4
投票

如果没有可重复的示例,则无法测试您的代码,但很可能您可以将ggplot调用移动到base_layer参数,以便您可以继续添加geoms。

ggmap(fr, base_layer = ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))) + 
    geom_raster(aes(fill = var1.pred), alpha = 0.5) +
    scale_fill_gradient(low = "white", high = "blue")+
    geom_contour(colour = "white", binwidth = 1) +
    labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')
© www.soinside.com 2019 - 2024. All rights reserved.