Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inset map using ggplot2

I'm just trying to make a simple study area map that also contains an inset of the state that I am working in (North Carolina). I would like to convert the inset map to a grob object to plot it within the main study area map, then use ggsave to save the map as an image, pdf, etc. I am using shapefiles for my actual map, but I'll show you what I am trying for using map_data:

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library(gridExtra)
library(grid)

# get the NC data:
states <- map_data("state")
nc_df <- subset(states, region == "north carolina")

# study area map:
nc_base <- ggplot() + 
geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black") +
coord_fixed(xlim=c(-80, -77.5), ylim=c(33.5, 34.9), ratio = 1.3) +
theme_bw()
nc_base

# inset map:
insetmap<-ggplot() + 
geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black")  + # get the state border back on top
coord_fixed(ratio = 1.3) +
annotate(geom = "rect", ymax = 34.9, ymin = 33.5, xmax = -77.5, xmin = -80, colour = "red", fill = NA) +
ylab("") +
xlab("") +
theme_nothing()
insetmap

insetmap.grob <- ggplotGrob(insetmap)

final_map <- nc_base + annotation_custom(insetmap.grob, xmin=-79.5, xmax=-79, ymin=33.75, ymax=34)
final_map

When I run the script to produce the final map, only the study area map is produced. I'm wondering if I'm using ggplotGrob incorrectly, or it is something else? I might have read elsewhere that the annotation_custom function does not work unless you are using the coord_cartesian function in ggplot2 (and here I am using coord_fixed). If that is the case, can I zoom in similarly with that function, or is there another coord_ function to zoom in on my study area map?

Thanks, Jay

like image 725
Jason Avatar asked Dec 13 '15 19:12

Jason


People also ask

What is an inset map?

An inset map is a smaller map to help the user maintain a geographical frame of reference. The overview map is synchronized with the bounds_changed event on the main map.

What is inset in R?

The optional inset argument specifies how far the legend is inset from the plot margins. If a single value is given, it is used for both margins; if two values are given, the first is used for x - distance, the second for y -distance.

How do I add a map to Qgis?

Go to Layout-> Add a Map. Once the Add map button is active, hold the left mouse and drag a rectangle where you want to insert the map. You will see that the rectangle window will be rendered with the map from the main QGIS canvas. You can see in the far right end the Items box; this shows you the map you just added.


1 Answers

I do this kind of thing fairly often and have found that the grid::viewport approach works well...although note that you cannot use ggsave when you are working with multiple viewports as ggsave will only save the last viewport. Try:

nc_base <- ggplot() + 
  geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black") +
  coord_fixed(xlim=c(-80, -77.5), ylim=c(33.5, 34.9), ratio = 1.3) +
  theme_bw()
print(nc_base)

# inset map:
insetmap <- ggplot() + 
  geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black")  + # get the state border back on top
  coord_fixed(ratio = 1.3) +
  annotate(geom = "rect", ymax = 34.9, ymin = 33.5, xmax = -77.5, xmin = -80, colour = "red", fill = NA) +
  ylab("") +
  xlab("") + 
# used theme_inset instead of theme_nothing
  theme_inset()
print(insetmap)

# save where you want to with filename arg in png(). Currently saves 'map.png' to your working directory
# set resolution, width, height
png(filename = "map.png", width = 1150, height = 800, res = 300)
# create a viewport for inset
# vp_inset width/height arguments set the size of the inset; x and y arguments set the position (from 0 to 1) of the left, top corner of the inset along each axis (i.e. not map coordinates as you have in your annotation custom). You can adjust these as you see fit.
vp_inset <- grid::viewport(width = 0.35, height = 0.35, x = 0.2, y = 0.5, just = c("left", "top"))
print(nc_base)
print(insetmap, vp = vp_inset)
dev.off()

enter image description here

like image 85
sebdalgarno Avatar answered Oct 04 '22 06:10

sebdalgarno