<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/"></div></div><div><a data-url="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/" data-counturl="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/" data-count="horizontal" data-text="Creating inset maps using spatial objects" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jannesm.wordpress.com/2016/07/05/inset-maps-using-spatial-objects/"> R – jannesm</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<div>
<p><img alt="meuse" class="wp-image-33" src="http://i0.wp.com/jannesm.files.wordpress.com/2016/07/meuse.png?w=456&ssl=1" data-recalc-dims="1">A while ago Arnold explained in his <a target="_blank" href="http://www.r-bloggers.com/creating-inset-map-with-ggplot2/">post</a> how to create an inset map using ggplot2. This is great but I have to admit I rarely use ggplot2 in combination with spatial data. Instead I often find myself using the plot functions provided by the raster- and sp-package. To create an inset map with these plot methods, we have to slightly adjust Arnold’s code.</p>
<div>
<p></p>
<p></p>
<p>First of all, let us attach some packages and data.</p>
<pre><code># attach packages
library(&quot;sp&quot;)
library(&quot;raster&quot;)
library(&quot;grid&quot;)
library(&quot;gridBase&quot;)
library(&quot;TeachingDemos&quot;)
library(&quot;rworldmap&quot;)
library(&quot;RColorBrewer&quot;)
library(&quot;classInt&quot;)

# attach country polygons
data(countriesLow)
cous &lt;- countriesLow
# find the Netherlands
net &lt;- cous[which(cous@data$NAME == &quot;Netherlands&quot;), ]  

# load meuse.riv
data(meuse.riv)
# convert to SpatialPolygons
riv &lt;- SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), ID = &quot;1&quot;)))

# meuse dataset
data(meuse)
coordinates(meuse) &lt;- c(&quot;x&quot;, &quot;y&quot;)
proj4string(meuse) &lt;- CRS(&quot;+init=epsg:28992&quot;)
# classifying cadmium into 5 classes
q_5 &lt;- classIntervals(meuse@data$cadmium, n = 5, style = &quot;fisher&quot;)
pal &lt;- brewer.pal(5, &quot;Reds&quot;)
my_cols &lt;- findColours(q_5, pal)
# we also need lat/lon coordinates
meuse_tr &lt;- spTransform(meuse, proj4string(net))
</code></pre>
<p>Next, I create the main plot and subsequently add the inset map.</p>
<pre><code># create the figure
png(file = &quot;meuse.png&quot;, w = 1800, h = 1800, res = 300)
plot.new()
vp_1 &lt;- viewport(x = 0, y = 0, width = 0.91, height = 1,
                just = c(&quot;left&quot;, &quot;bottom&quot;))
vp_2 &lt;- viewport(x = 0.61, y = 0.19, width = 0.22, height = 0.25,
                just = c(&quot;left&quot;, &quot;bottom&quot;))

# main plot
pushViewport(vp_1)
par(new = TRUE, fig = gridFIG())
plot(raster::crop(riv, bbox(meuse) + c(-500, -1000, 2000, 2000)), axes = TRUE,
     col = &quot;lightblue&quot;, xlim = c(178500, 182000), ylim = c(329000, 334000))
plot(meuse, col = &quot;black&quot;, bg = my_cols, pch = 22, add = TRUE)
legend(&quot;topleft&quot;, fill = attr(my_cols, &quot;palette&quot;), 
       legend = names(attr(my_cols, &quot;table&quot;)), bty = &quot;n&quot;)
upViewport()

# inset map
pushViewport(vp_2)
par(new = TRUE, fig = gridFIG(), mar = rep(0, 4))
# plot the Netherlands and its neighbors
plot(cous[net, ], xlim = c(4.2, 5.8), ylim = c(50, 53.7), 
     col = &quot;white&quot;, bg = &quot;transparent&quot;)
plot(net, col = &quot;lightgray&quot;, add = TRUE)
# add the study area location
points(x = coordinates(meuse_tr)[1, 1], y = coordinates(meuse_tr)[1, 2], 
       cex = 1.5, pch = 15)
shadowtext(x = coordinates(meuse_tr)[1, 1] - 0.35, 
           y = coordinates(meuse_tr)[1, 2] - 0.1,
           labels = &quot;study         \n area&quot;, font = 3)
dev.off()</code></pre>
</div>
<p> </p>
<p></p>
<p> </p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jannesm.wordpress.com/25/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/jannesm.wordpress.com/25/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/"></div></div><div><a data-url="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/" data-counturl="http://www.r-bloggers.com/creating-inset-maps-using-spatial-objects/" data-count="vertical" data-text="Creating inset maps using spatial objects" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://jannesm.wordpress.com/2016/07/05/inset-maps-using-spatial-objects/"> R – jannesm</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/u-SKGsS6UE4"></a></div>