简体   繁体   中英

How to join ggplot (having lon, lat and fill value) with ggmap?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM