Pages

Sunday, June 15, 2014

Creating Inset Map with ggplot2

According to wiki.GIS.com one of the reason for using inset map is to provide a reference for an area for unfamiliar readers.  Inset map is also considered a great asset for cartographers.  Most of the GIS software available in the market have a provision for non-cartographer and beginners to create inset map. However, for R users who are into making maps, creating inset map is a bit challenging. Thanks to the post of Pascal Mickelson and Scott Chamberlain which gave users like me a guide on how to create inset map in R using ggplot2. Below is an example of map with inset created using R.

##################################
# Creating Inset Maps in ggplot2 #
##################################
library(ggplot2)
library(raster)
library(gridExtra)
ph0<-getData("GADM", country="PHL", level=0) # download PHL level 0 map for ucdavis site
phl<-getData("GADM", country="PHL", level=2) # download PHL level 2 map for ucdavis site
mrdq<-(phl[phl$NAME_1=="Marinduque",]) # subset province of Marinduque from PHL map
munnames<-coordinates(mrdq) # get center coordinates of municipalities of Marinduque
munnames<-data.frame(munnames) # convert matrix format munnames object to data.frame
munnames$label<-mrdq@data$NAME_2
# Extent rectangle for inset map
pol<-data.frame(xmin=121.7,xmax=122.2 ,ymin=13 ,ymax=13.7)
# Main Map
p1<-ggplot()+geom_polygon(data=mrdq, aes(long+0.008,lat-0.005, group=group), fill="#9ecae1")+
geom_polygon(data=mrdq, aes(long,lat, group=group), colour="grey10",fill="#fff7bc")+
geom_text(data=munnames, aes(x=X1, y=X2,label=label), size=3, colour="grey20")+
coord_equal()+theme_bw()+xlab("")+ylab("")+
scale_x_continuous(breaks=seq(121.8,122.2, 0.1), labels=c(paste(seq(121.8,122.2, 0.1),"°E", sep="")))+
scale_y_continuous(breaks=seq(13.2,13.6, 0.1), labels=c(paste(seq(13.2,13.6, 0.1),"°N", sep="")))+
theme(axis.text.y =element_text(angle = 90, hjust=0.5))
#Inset
p2<-ggplot()+geom_polygon(data=ph0, aes(long,lat,group=group),colour="grey10",fill="#fff7bc")+
coord_equal()+theme_bw()+labs(x=NULL,y=NULL)+
scale_x_continuous(breaks=seq(117.5,125, 2.5), labels=c(paste(seq(117.5,125, 2.5),"°E", sep="")))+
scale_y_continuous(breaks=seq(5,20, 5), labels=c(paste(seq(5,20, 5),"°N", sep="")))+
geom_rect(data = pol, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha=0, colour="red", size = 1, linetype=1)+
theme(axis.text.x =element_blank(),axis.text.y= element_blank(), axis.ticks=element_blank(),axis.title.x =element_blank(),
axis.title.y= element_blank())
png(file="mrdq.png",w=1800,h=1800, res=300)
grid.newpage()
v1<-viewport(width = 1, height = 1, x = 0.5, y = 0.5) #plot area for the main map
v2<-viewport(width = 0.3, height = 0.3, x = 0.86, y = 0.28) #plot area for the inset map
print(p1,vp=v1)
print(p2,vp=v2)
dev.off()
view raw Inset.r hosted with ❤ by GitHub

5 comments:

  1. AnonymousJune 16, 2014

    Nice one! Thanks for sharing.

    Something that I've wrangled with once or twice and just gave up on and used QGIS instead.

    ReplyDelete
  2. Thanks.

    I have also one question: Do you know the way to delete the white area around inset map?

    ReplyDelete
  3. Hi Nowgis,


    See the link below:

    http://stackoverflow.com/questions/21027548/cropping-extra-white-space-in-a-plot-made-using-ggplot-package-in-r

    ReplyDelete
  4. Hi Arnold,

    This is exactly what I've been looking for!
    Unfortunately, between the time of you posting this and today there must have been some changes made to the code base - as I can't get it to work: I keep getting the following message "Regions defined for each Polygons" after p1. I've looked online for a solution but still not managed to get it to work!
    Any chance you may be able to help!?

    Many thanks and best wishes

    ReplyDelete
    Replies
    1. Hi Coco,

      Try

      type the code print(p1) to view the map, if that's what you want to do. Or just type p1.

      Hope this helps.

      Delete