ggplot2 maps with insets

[This article was first published on Recology - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here’s a quick demo of creating a map with an inset within it using ggplot. The inset is achieved using the gridExtra package.

Install libraries, set directory, read file

setwd<span class="p">(</span><span class="s">"/Users/ScottMac/Dropbox/CANPOLIN_networks_ms/data"</span><span class="p">)</span>  <span class="c1"># change to your directory</span>
library<span class="p">(</span>ggplot2<span class="p">)</span>
library<span class="p">(</span>maps<span class="p">)</span>
library<span class="p">(</span>maptools<span class="p">)</span>
library<span class="p">(</span>gridExtra<span class="p">)</span>
library<span class="p">(</span>rgeos<span class="p">)</span>
dat <span class="o"><-</span> read.csv<span class="p">(</span><span class="s">"siteinfo_blog.csv"</span><span class="p">)</span>
head<span class="p">(</span>dat<span class="p">)</span>
  ecosystem   lat    lon
1  oak sav. 48.81 -123.6
2  oak sav. 48.79 -123.6
3  oak sav. 48.82 -124.1
4  oak sav. 48.82 -124.1
5  oak sav. 48.78 -123.9
6  oak sav. 48.78 -123.9

Get maps

<span class="c1"># Get a map of Canada</span>
canadamap <span class="o"><-</span> data.frame<span class="p">(</span>map<span class="p">(</span><span class="s">"world"</span><span class="p">,</span> <span class="s">"Canada"</span><span class="p">,</span> plot <span class="o">=</span> <span class="kc">FALSE</span><span class="p">)[</span>c<span class="p">(</span><span class="s">"x"</span><span class="p">,</span> <span class="s">"y"</span><span class="p">)])</span>

<span class="c1"># Get a map of smaller extent</span>
canadamapsmall <span class="o"><-</span> canadamap<span class="p">[</span>canadamap<span class="p">$</span>x <span class="o"><</span> <span class="m">-90</span> <span class="o">&</span> canadamap<span class="p">$</span>y <span class="o"><</span> <span class="m">54</span><span class="p">,</span> <span class="p">]</span>

Make the maps

<span class="c1"># The inset map, all of Canada</span>
a <span class="o"><-</span> ggplot<span class="p">(</span>canadamap<span class="p">)</span> <span class="o">+</span> 
    theme_bw<span class="p">(</span>base_size <span class="o">=</span> <span class="m">22</span><span class="p">)</span> <span class="o">+</span>
    geom_path<span class="p">(</span>data <span class="o">=</span> canadamap<span class="p">,</span> aes<span class="p">(</span>x<span class="p">,</span> y<span class="p">),</span> colour <span class="o">=</span> <span class="s">"black"</span><span class="p">,</span> fill <span class="o">=</span> <span class="s">"white"</span><span class="p">)</span> <span class="o">+</span>
    scale_size<span class="p">(</span>guide<span class="o">=</span><span class="s">"none"</span><span class="p">)</span> <span class="o">+</span>
    opts<span class="p">(</span>panel.border <span class="o">=</span> theme_rect<span class="p">(</span>colour <span class="o">=</span> <span class="s">'black'</span><span class="p">,</span> size <span class="o">=</span> <span class="m">1</span><span class="p">,</span> linetype<span class="o">=</span><span class="m">1</span><span class="p">),</span>
             panel.grid.major <span class="o">=</span> theme_blank<span class="p">(),</span> panel.grid.minor<span class="o">=</span>theme_blank<span class="p">(),</span>
             panel.background <span class="o">=</span> theme_rect<span class="p">(</span> fill <span class="o">=</span> <span class="s">'white'</span><span class="p">),</span>
             legend.position <span class="o">=</span> c<span class="p">(</span><span class="m">0.15</span><span class="p">,</span><span class="m">0.80</span><span class="p">),</span> legend.key <span class="o">=</span> theme_blank<span class="p">(),</span>
             axis.ticks <span class="o">=</span> theme_blank<span class="p">(),</span> axis.text.x<span class="o">=</span>theme_blank<span class="p">(),</span>
             axis.text.y<span class="o">=</span>theme_blank<span class="p">())</span> <span class="o">+</span>
    labs<span class="p">(</span>x <span class="o">=</span> <span class="s">''</span><span class="p">,</span> y <span class="o">=</span> <span class="s">''</span><span class="p">)</span>

<span class="c1"># The larger map, zoomed in, with the data</span>
b <span class="o"><-</span> ggplot<span class="p">(</span>dat<span class="p">,</span> aes<span class="p">(</span>lon<span class="p">,</span> lat<span class="p">,</span> colour<span class="o">=</span>ecosystem<span class="p">))</span> <span class="o">+</span>
    theme_bw<span class="p">(</span>base_size <span class="o">=</span> <span class="m">22</span><span class="p">)</span> <span class="o">+</span>
    geom_jitter<span class="p">(</span>size<span class="o">=</span><span class="m">4</span><span class="p">,</span> alpha<span class="o">=</span><span class="m">0.6</span><span class="p">)</span> <span class="o">+</span>
    geom_path<span class="p">(</span>data <span class="o">=</span> canadamapsmall<span class="p">,</span> aes<span class="p">(</span>x<span class="p">,</span> y<span class="p">),</span> colour <span class="o">=</span> <span class="s">"black"</span><span class="p">,</span> fill <span class="o">=</span> <span class="s">"white"</span><span class="p">)</span> <span class="o">+</span>
    scale_size<span class="p">(</span>guide<span class="o">=</span><span class="s">"none"</span><span class="p">)</span> <span class="o">+</span>
    opts<span class="p">(</span>panel.border <span class="o">=</span> theme_rect<span class="p">(</span>colour <span class="o">=</span> <span class="s">'black'</span><span class="p">,</span> size <span class="o">=</span> <span class="m">1</span><span class="p">,</span> linetype<span class="o">=</span><span class="m">1</span><span class="p">),</span>
             panel.grid.major <span class="o">=</span> theme_blank<span class="p">(),</span> panel.grid.minor<span class="o">=</span>theme_blank<span class="p">(),</span>
             panel.background <span class="o">=</span> theme_rect<span class="p">(</span> fill <span class="o">=</span> <span class="s">'white'</span><span class="p">),</span>
             legend.position <span class="o">=</span> c<span class="p">(</span><span class="m">0.1</span><span class="p">,</span><span class="m">0.20</span><span class="p">),</span> legend.text<span class="o">=</span>theme_text<span class="p">(</span>size<span class="o">=</span><span class="m">12</span><span class="p">,</span> face<span class="o">=</span><span class="s">'bold'</span><span class="p">),</span> 
             legend.title<span class="o">=</span>theme_text<span class="p">(</span>size<span class="o">=</span><span class="m">12</span><span class="p">,</span> face<span class="o">=</span><span class="s">'bold'</span><span class="p">),</span> legend.key <span class="o">=</span> theme_blank<span class="p">(),</span>
             axis.ticks <span class="o">=</span> theme_segment<span class="p">(</span>size <span class="o">=</span> <span class="m">2</span><span class="p">))</span> <span class="o">+</span>
    labs<span class="p">(</span>x <span class="o">=</span> <span class="s">''</span><span class="p">,</span> y <span class="o">=</span> <span class="s">''</span><span class="p">)</span>

Print the two maps together, one an inset of the other

This approach uses the gridExtra package for flexible alignment, etc. of ggplot graphs

grid.newpage<span class="p">()</span>
vpb_ <span class="o"><-</span> viewport<span class="p">(</span>width <span class="o">=</span> <span class="m">1</span><span class="p">,</span> height <span class="o">=</span> <span class="m">1</span><span class="p">,</span> x <span class="o">=</span> <span class="m">0.5</span><span class="p">,</span> y <span class="o">=</span> <span class="m">0.5</span><span class="p">)</span>  <span class="c1"># the larger map</span>
vpa_ <span class="o"><-</span> viewport<span class="p">(</span>width <span class="o">=</span> <span class="m">0.4</span><span class="p">,</span> height <span class="o">=</span> <span class="m">0.4</span><span class="p">,</span> x <span class="o">=</span> <span class="m">0.8</span><span class="p">,</span> y <span class="o">=</span> <span class="m">0.8</span><span class="p">)</span>  <span class="c1"># the inset in upper right</span>
print<span class="p">(</span>b<span class="p">,</span> vp <span class="o">=</span> vpb_<span class="p">)</span>
print<span class="p">(</span>a<span class="p">,</span> vp <span class="o">=</span> vpa_<span class="p">)</span>

center


Written in Markdown, with help from knitr, and nice knitr highlighting/etc. in in RStudio.

To leave a comment for the author, please follow the link and comment on their blog: Recology - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)