如何使用ggmap加入ggplot(具有lon,lat和fill值)? [英] How to join ggplot (having lon, lat and fill value) with ggmap?

查看:266
本文介绍了如何使用ggmap加入ggplot(具有lon,lat和fill值)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要有一张地图,用一定比例显示该地区的价值分布。这是我的数据框:

 头(旅行)
提示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。框架(行程))+ 
geom_tile()+
scale_fill_continuous(low =white,high =blue)+
coord_equal()

并且这也会生成并显示出来。



问题出在我想要做的时候这:

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

我收到以下错误消息:

  p + o中的错误:二元运算符的非数字参数
另外:警告消息:
不兼容的方法.gg,Ops.data.frame)为+

我将不胜感激你可以帮助我加入这两个对象。

解决方案看看你的数据,最好使用 geom_point 而不是 geom_tile 。原因是这种数据点在地图上更好地可见。



如何结合 ggmap
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
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.97 72 40.7559
1.268699 -73.9932 40.7628
1.265304 -73.9932 40.7429
1.193437 -73.9852 40.7447,header = TRUE)


I want to have a map presenting value distribution over the area with some scale. Here is my data frame:

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

I have generated a map using following code:

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

That map is properly displayed.

I have also generated a layer with my data representation:

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

And this is also generated and displayed.

The issue is when I want to do this:

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

I am getting following error:

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

I would be grateful if you could help me to join these two objects.

解决方案

Looking at your data, it is probably better to use geom_point instead of geom_tile. The reason for this is that this kind of data points are better visible on a map.

An example of how to combine ggmap and ggplot into one plot:

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")

this piece of example code gives the following map:

In the above code I used position="jitter" because two of the points are overlapping.

Used data:

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)

这篇关于如何使用ggmap加入ggplot(具有lon,lat和fill值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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