【问题标题】:How to join ggplot (having lon, lat and fill value) with ggmap?如何使用 ggmap 加入 ggplot(具有 lon、lat 和填充值)?
【发布时间】:2015-09-25 22:55:53
【问题描述】:

我想要一张地图,以某种比例显示该区域的价值分布。这是我的数据框:

head(trip)
   TIP      LON     LAT
1 1.318878 -73.9932 40.7223
2 1.370667 -73.9932 40.7222
3 0.933232 -73.9772 40.7559
4 1.268699 -73.9932 40.7628
5 1.265304 -73.9932 40.7429
6 1.193437 -73.9852 40.7447

我使用以下代码生成了一张地图:

map <- ggmap::get_map("new york", zoom = 11)
nyc_map <- ggmap::ggmap(map, legend="topleft")

该地图已正确显示。

我还用我的数据表示生成了一个层:

ggplot(aes(LON,LAT, fill = TIP), data=as.data.frame(trip)) + 
geom_tile() + 
scale_fill_continuous(low="white", high="blue") + 
coord_equal()

这也是生成和显示的。

问题是我什么时候想这样做:

nyc_map + 
ggplot(aes(LON,LAT, fill = TIP), data=as.data.frame(trip)) + 
geom_tile() + 
scale_fill_continuous(low="white", high="blue") + 
coord_equal()

我收到以下错误:

Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+" 

如果您能帮我加入这两个对象,我将不胜感激。

【问题讨论】:

    标签: r ggplot2 ggmap


    【解决方案1】:

    查看您的数据,最好使用geom_point 而不是geom_tile。原因是这种数据点在地图上更容易看到。

    如何将ggmapggplot 组合成一个情节的示例:

    library(ggplot2)
    library(ggmap)
    
    nyc_map <- get_map("new york", zoom = 12, maptype = "hybrid")
    
    ggmap(nyc_map) + 
      geom_point(data=trip, aes(x=LON, y=LAT, fill=TIP), size=6, shape=21, position="jitter") + 
      scale_fill_continuous(low="white", high="blue")
    

    这段示例代码给出了以下地图:

    在上面的代码中,我使用了position="jitter",因为其中两个点是重叠的。

    使用过的数据:

    trip <- read.table(text="TIP      LON     LAT
    1.318878 -73.9932 40.7223
    1.370667 -73.9932 40.7222
    0.933232 -73.9772 40.7559
    1.268699 -73.9932 40.7628
    1.265304 -73.9932 40.7429
    1.193437 -73.9852 40.7447", header=TRUE)
    

    【讨论】:

      猜你喜欢
      • 2020-05-26
      • 2012-01-08
      • 2021-12-10
      • 1970-01-01
      • 2018-09-05
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多