Example of sampling location map using Google Maps and a Second Inset Map

library(ggplot2)
library(ggmap)
library(maps)
library(grid)
library(gridExtra)
library(sp)
library(lattice)
library(map tools)

#get google map of Rio Bonito/Silva Jardim area with embedded capture pointed
rj2=get_googlemap(center=c(lon=-42.482, lat=-22.65), zoom=11,maptype="terrain", 
markers="&markers=color:blue|label:1|-22.684114,-42.485386&markers=color:blue|label:2| 
-22.632589,-42.467131", format="png8", filename="your file")

#turn google map into a ggmap object and get lat/long axes on the map
rj2_axes=ggmap(rj2)+geom_point(aes(x=lon,y=lat))
rj2_axes

#code to get inset map into larger map
brazilmap <- data.frame(map("world", "Brazil", plot = FALSE)[c("x", "y")])
# This should get your corner points for the box, picking min and max of the axes for rj2_axes map
insetrect <- data.frame(xmin = min(-42.7), xmax = max(-42.3),
ymin = min(-22.85), ymax = max(-22.45))
# The inset map, all of Brazil
a <- ggplot(brazilmap) +theme_bw(base_size = 22) +geom_path(data = brazilmap, aes(x, y),colour = "black", fill = "white") + theme(panel.border = element_rect(colour = 'black',size = 1, linetype=1),panel.grid.major = element_blank(), panel.grid.minor=element_blank(),panel.background = element_rect( fill = 'white'),legend.position = c(0.15,0.80), legend.key =element_blank +(),axis.ticks = element_blank(), axis.text.x=element_blank(),axis.text.y=element_blank()) + labs(x = '', y ='')+geom_rect(+data = insetrect, aes(xmin = xmin, xmax = xmax,ymin = ymin, ymax = ymax), alpha=0, colour='blue', size = 2, linetype=1)

#print the two maps together
vpa_ <- viewport(width = 0.35, height = 0.35, x = 0.22, y = 0.8) # for inset map
rj2_axes+geom_rect(aes(xmin=-42.420, xmax=-42.28, ymin=-22.84, ymax=-22.74), fill='light blue', alpha=0.6)+geom_text(x=-42.35, y=-22.75, label='Capture Locations:', size=3, font= "comic sans")+geom_text(x=-42.35,y=-22.76, label="1. Point 1", size=3, font="comic sans")+geom_text(x=-42.35,y=-22.77, label="2. Point 2", size=3, font="comic sans")
print(a, vp = vpa_)

Leave a comment