0

I have two plots that I would like to overlay in a particular way. Instead of side by side like when using par(), I would like one to sit inside the other, but be about a quarter the size.

More details: one of my plots is a map, another is a scatterplot with colored quadrants. The colored quadrants represent the colors plotted onto the map, so I would like to inset it nicely in the same plot as the map so that it serves as a legend.

Thanks in advance

royburst
  • 73
  • 1
  • 5
  • depends, what are you doing to generate either plot? using lattice graphics or grid graphics or ggplot or ... ? – mathematical.coffee Feb 05 '14 at 04:02
  • [This link](http://recology.info/2012/08/ggplot-inset-map/) provides an example of map insets with ggplot, and [this](http://stackoverflow.com/questions/17041246/) link provides an example with `plot(...)` in base R. – jlhoward Feb 05 '14 at 04:03
  • Something like this: http://stackoverflow.com/questions/10369253/overlaying-r-county-map-onto-a-state-map ? – thelatemail Feb 05 '14 at 04:03
  • Greg Snow wrote such a "subplot" function in his TeachingDemos package: http://stackoverflow.com/questions/15226313/drawing-a-graph-in-the-corner-of-another/15233208#15233208 – IRTFM Feb 05 '14 at 06:22
  • There's also [this approach](http://wiki.cbr.washington.edu/qerm/index.php/R/Making_Maps). – jbaums Feb 05 '14 at 06:46

2 Answers2

0

Here's an example, although the links in comments point to similar approaches.

Grab a shapefile:

download.file(file.path('http://www.naturalearthdata.com/http/',
                        'www.naturalearthdata.com/download/50m',
                        'cultural/ne_50m_admin_1_states_provinces_lakes.zip'),
              {f <- tempfile()})
unzip(f, exdir=tempdir())

Plotting:

library(rgdal)
shp <- readOGR(tempdir(), 'ne_50m_admin_1_states_provinces_lakes')
plot(subset(shp, admin=='Australia'), 
     col=sample(c('#7fc97f', '#beaed4', '#fdc086', '#ffff99'), 
                9, repl=TRUE))
opar <- par(plt=c(0.75, 0.95, 0.75, 0.95), new=TRUE)
plot.new()
plot.window(xlim=c(0, 1), ylim=c(0, 1), xaxs='i', yaxs='i')
rect(0, 0, 0.5, 0.5, border=NA, col='#7fc97f')
rect(0.5, 0, 1, 0.5, border=NA, col='#beaed4')
rect(0, 0.5, 0.5, 1, border=NA, col='#fdc086')
rect(0.5, 0.5, 1, 1, border=NA, col='#ffff99')
points(runif(100), runif(100), pch=20, cex=0.8)
box(lwd=2)
par(opar)

See plt under ?par for clarification.

map with inset

jbaums
  • 27,115
  • 5
  • 79
  • 119
0

This is how I did it in the past

grid.newpage()
vp <- viewport(width = 1, height = 1)
submain <- viewport(width = 0.9, height = 0.9, x = 0.5, y = 1,just=c("center","top"))
print(p, vp = submain)
subvp2 <- viewport(width = 0.2, height = 0.2, x = 0.39, y = 0.35,just=c("right","top"))
print(hi, vp = subvp2)
subvp1 <- viewport(width = 0.28, height = 0.28, x = 0.0, y = 0.1,just=c("left","bottom"))
print(ak, vp = subvp1)

in my case p, ak and hi were gg objects (maps created with ggplot) and I was inserting a small version of each near the main use map (p) - as it is typically done

user1617979
  • 2,370
  • 3
  • 25
  • 30