Generating form files with ggmap: clipping when the form file is larger than ggmap

I have clipping problems when I try to combine ggmap with form files. The example in Kahle and Wickham (2013: 158) works great because the bitmap from ggmap spans the entire form file. The following is an example of what happens when I try to build a form file for the US states on a ggmap chart that covers a smaller area. Ggmap shows New York, and I want to lay it on the borders for the US states (as an example). The resulting card does not make any sense. The problem is that the form file is cropped and ggplot joins loose points. Below is the code. The form file is here . I just show the latest plot here.

How can I solve this problem?

path <- "PATH TO SHAPEFILE" library("ggmap") library("rgdal") # shapefile states <- readOGR(dsn = path, layer = "states") states_df <- fortify(states) # plot shapefile plot(states, lwd = 0.1) ggplot(states_df, aes(long, lat, group = group)) + geom_polygon(colour = "black", fill = NA, size = 0.1) # combine ggmap with shapefile map <- get_map("new york city", zoom = 10, source = "stamen") ggmap(map, extent = "device") ggmap(map, extent = "device") + geom_polygon(aes(long, lat, group=group), data = states_df, colour = "red", fill = NA, size = 1) 
Kale, David and Hadley Wickham. 2013. "Ggmap: Spatial Visualization with ggplot2." R Journal 5 (1): 144-61.

enter image description here

+6
source share
2 answers

Here is my attempt. I often use GADM shapefiles, which you can directly import using the raster package. I have expanded the form file for NY, NJ and CT. You may not have to do this at the end, but it is probably best to reduce the amount of data. When I drew the map, ggplot automatically deleted the data points that remain outside the bbox of the ggmap image. Therefore, I did not have to do any additional work. I'm not sure which shapefile you used. But GADM data seems to work well with ggmap images. Hope this helps you.

 library(raster) library(rgdal) library(rgeos) library(ggplot2) ### Get data (shapefile) us <- getData("GADM", country = "US", level = 1) ### Select NY and NJ states <- subset(us, NAME_1 %in% c("New York", "New Jersey", "Connecticut")) ### SPDF to DF map <- fortify(states) ## Get a map mymap <- get_map("new york city", zoom = 10, source = "stamen") ggmap(mymap) + geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group)) 

enter image description here

If you just need strings, the following will be what you need.

 ggmap(mymap) + geom_path(data = map, aes(x = long, y = lat, group = group)) 

enter image description here

+3
source

I would look at this answer, it seems that ggmap, as you expected, is not ideal for a polygon when zoomed in, namely, non-plot elements are truncated, causing "interesting" results regarding the form files,

Polygons crop ggplot2 / ggmap perfectly at different zoom levels

 # transform for good measure states <- spTransform(states,CRS("+datum=WGS84 +proj=longlat") ) # combine ggmap with shapefile states_df <- fortify(states) # get your map map <-get_map("new york city", zoom = 10, source = "stamen") a <- ggmap(map, # this is where we get our raster base_layer=ggplot(aes(x=long, y=lat), data=states_df), # this defines the region where things are plotted extent = "normal", # this won't work with device, you need normal (see examples in ggmap documentation) maprange=FALSE ) + coord_map( # use map bounding box to setup the 'viewport' we want to see projection="mercator", xlim= c(attr(map, "bb")$ll.lon, attr(map, "bb")$ur.lon), ylim=c(attr(map, "bb")$ll.lat, attr(map, "bb")$ur.lat) ) + geom_polygon( # plot the polygon aes(x=long, y=lat,group=group), data =states_df, color = "red", fill=NA, size = 1) print(a) 

With an exit: enter image description here

As an additional note that you can check using the US Census data for state maps, they appear to be of higher quality than the ESRI dataset.

ftp://ftp2.census.gov/geo/pvs/tiger2010st/tl_2010_us_state10.zip

As a final note, there are problems with ggmap near the poles, so I will also multiply your data on the states of interest to you.

+2
source

Source: https://habr.com/ru/post/985786/


All Articles