Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Forked from oscarperpinan/inset.R
Created June 2, 2017 05:02
Show Gist options
  • Save johnbaums/c44a0423cd88da27f19a85846d4121ad to your computer and use it in GitHub Desktop.
Save johnbaums/c44a0423cd88da27f19a85846d4121ad to your computer and use it in GitHub Desktop.
Inset graphics with rasterVis and grid
library(raster)
library(rasterVis)
library(grid)
r <- raster(system.file("external/test.grd", package="raster"))
## Main graphic
p1 <- levelplot(r)
## Inset graphic
p2 <- levelplot(r,
margin = FALSE,
colorkey = FALSE,
scales = list(draw = FALSE),
par.settings = GrTheme)
## Create viewports (more info in https://stat.ethz.ch/R-manual/R-devel/library/grid/doc/grid.pdf)
## Main viewport
vp1 <- viewport()
pushViewport(vp1)
print(p1, newpage = FALSE)
## Inset viewport
xi <- 0.3
yi <- 0.6
wi <- 0.3
hi <- 0.2
vp2 <- viewport(x = xi,
y = yi,
height = hi,
width = wi)
pushViewport(vp2)
print(p2, newpage=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment