inset map using ggplot2
Asked Answered
R

1

8

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

Rhizo answered 13/12, 2015 at 19:47 Comment(2)
Does this or this help?Calumniate
@Rhizo did you fix this problem ? If so how. I am having exactly same problem. ThanksPolson
C
6

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

Cashandcarry answered 28/2, 2018 at 1:8 Comment(1)
can you please assist me on how to do part of multilines graph (four lines), zooming part of the graph and insetting it using R, I really appreciate and I am having difficulty!Freeboard

© 2022 - 2024 — McMap. All rights reserved.