The Tile Grid Map for Canada

[This article was first published on max humber, 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.

I love the tile grid map:

center

Source

I think they’re elegant, easy to read, and great for data that are population agnostic.

I live in Canada, though. And it sucks (well, no it doesn’t!) because I’ve only ever seen tile grids for America.

I decided that Canada deserved one.

So, here is the tile grid map template for all you Canadian useRs:

<span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">viridis</span><span class="p">)</span><span class="w">

</span><span class="c1"># LOCATION DATAFRAME
</span><span class="w">
</span><span class="n">canada</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tribble</span><span class="p">(</span><span class="w">
    </span><span class="o">~</span><span class="n">province</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">code</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="n">y</span><span class="p">,</span><span class="w">
    </span><span class="s2">"Yukon"</span><span class="p">,</span><span class="w"> </span><span class="s2">"YK"</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w">
    </span><span class="s2">"British Columbia"</span><span class="p">,</span><span class="w"> </span><span class="s2">"BC"</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Northwest Territories"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NW"</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Alberta"</span><span class="p">,</span><span class="w"> </span><span class="s2">"AB"</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Nunavut"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NU"</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Saskatchewan"</span><span class="p">,</span><span class="w"> </span><span class="s2">"SK"</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w">
    </span><span class="s2">"Manitoba"</span><span class="p">,</span><span class="w"> </span><span class="s2">"MB"</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Ontario"</span><span class="p">,</span><span class="w"> </span><span class="s2">"ON"</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w">
    </span><span class="s2">"Quebec"</span><span class="p">,</span><span class="w"> </span><span class="s2">"QC"</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Prince Edward Island"</span><span class="p">,</span><span class="w"> </span><span class="s2">"PE"</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"New Brunswick"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NB"</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w">
    </span><span class="s2">"Newfoundland and Labrador"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NL"</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> 
    </span><span class="s2">"Nova Scotia"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NS"</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="p">)</span><span class="w">

</span><span class="c1"># YOUR DATA HERE
# just make sure province = province
</span><span class="w"> 
</span><span class="n">df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="p">(</span><span class="w">
    </span><span class="n">province</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Ontario"</span><span class="p">,</span><span class="w"> </span><span class="s2">"British Columbia"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Alberta"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Manitoba"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Nova Scotia"</span><span class="p">),</span><span class="w"> 
    </span><span class="n">`Fake Data`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">)</span><span class="w">
</span><span class="p">)</span><span class="w">

</span><span class="c1"># TILE GRID MAP
</span><span class="w">
</span><span class="n">canada</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">left_join</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"province"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    </span><span class="n">geom_tile</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`Fake Data`</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    </span><span class="n">geom_text</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">code</span><span class="p">),</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    </span><span class="n">coord_fixed</span><span class="p">(</span><span class="n">ratio</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    </span><span class="n">theme</span><span class="p">(</span><span class="w">
        </span><span class="n">panel.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">panel.grid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w"> 
        </span><span class="n">axis.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w"> 
        </span><span class="n">axis.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w"> 
        </span><span class="n">axis.ticks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">())</span><span class="w"> </span><span class="o">+</span><span class="w"> 
    </span><span class="n">scale_fill_viridis</span><span class="p">(</span><span class="n">na.value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#E1E1E1"</span><span class="p">,</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"D"</span><span class="p">,</span><span class="w"> </span><span class="n">begin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.2</span><span class="p">,</span><span class="w"> </span><span class="n">end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.8</span><span class="p">)</span><span class="w">
</span>

center

To leave a comment for the author, please follow the link and comment on their blog: max humber.

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)