<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simplifying-world-tile-grid-creation-with-geom_wtg/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/08/27/simplifying-world-tile-grid-creation-with-geom_wtg/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Nowadays (I’ve seen that word used <em>so</em> much in journal articles lately that I could not resist using it) I’m using world tile grids more frequently as the need arises to convey the state of exposure of various services at a global (country) scale. Given that necessity fosters invention it seemed that having a ggplot2 geom for world tile grids would make my life easier and also make work more efficient.</p>
<p>To that end, there’s a nascent ggplot2 extension package for making world tile grids called (uncreatively) <a rel="nofollow" href="https://gitlab.com/hrbrmstr/worldtilegrid" target="_blank"><code>worldtilegrid</code><img src="https://i1.wp.com/s.w.org/images/core/emoji/11/72x72/1f517.png?w=456&ssl=1" alt="ðŸ" class="wp-smiley" data-recalc-dims="1"></a>. It’s also at GitHub if you’re more comfortable working from code from there.</p>
<p>The <a rel="nofollow" href="https://gitlab.com/hrbrmstr/worldtilegrid#readme" target="_blank">README</a> has examples but we’ll walk through another one here and work with life expectancy data curated by <a rel="nofollow" href="https://ourworldindata.org/life-expectancy" target="_blank">Our World in Data</a>. Rather than draw out this post to a less tenable length with an explanation of how to pull the XHR JSON data into R, just grab the CSV linked with the visualization on that page and substitute the path in the code for where you stored it.</p>
<p>We do need to clean up this data a bit since it has some issues. Let’s do that and carve out some slices into two new data frames so we can work with the most recent curated year and some historical data:</p>
<pre><code>library(hrbrthemes) # gitlab/github :: hrbrmstr/hrbrthemes (use devtools installers)
library(worldtilegrid) # gitlab/github :: hrbrmstr/worldtilegrid (use devtools installers)
library(tidyverse)

cols(
  Entity = col_character(),
  Code = col_character(),
  Year = col_integer(),
  `Life expectancy (Clio-Infra up to 1949; UN Population Division for 1950 to 2015)` = col_double()
) -> le_cols

read_csv(&quot;~/Downloads/life-expectancy.csv&quot;, col_types = le_cols) %>%
  set_names(c(&quot;country&quot;, &quot;iso3c&quot;, &quot;year&quot;, &quot;life_expectancy&quot;)) -> lifexp

# clean it up a bit since it's not great data and bust it into groups
# that match the vis on the site vs use continuous raw data, esp since
# the data is really just estimates
filter(lifexp, !is.na(iso3c)) %>%
  filter(nchar(iso3c) == 3) %>%
  mutate(
    grp = cut(
      x = life_expectancy,
      breaks = c(10, 20, 30, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85),
      include.lowest = TRUE
    )
  ) -> lifexp

last_year &lt;-  filter(lifexp, year == last(year))
a_few_years &lt;- filter(lifexp, year %in% c(seq(1900, 2015, 20), 2015))</code></pre>
<p>Now we can use it to make some cartograms.</p>
<h3>Using the World Tile Grid geom</h3>
<p>You can <a rel="nofollow" href="https://rud.is/b/2018/06/07/making-world-tile-grid-grids/" target="_blank">reference a previous post</a> for what it would normally take to create these tile grids. The idea was to simplify the API down to the caller just needing to specify some country names/ISO[23]Cs (to a <code>country</code> aesthetic) and a value (to a <code>fill</code> aesthetic) and let the package do the rest. To that end, here's how to display the life expectancy data for 2015:</p>
<pre><code>ggplot(last_year) +
  geom_wtg(aes(country = iso3c, fill = grp), border_col = &quot;#2b2b2b&quot;) + # yep, you can sum up the entire blog post to this one line
  viridis::scale_fill_viridis(
    name = &quot;Life Expectancy&quot;, discrete = TRUE, na.value=alpha(ft_cols$gray, 1/10),
    drop = FALSE
  ) +
  coord_equal() +
  labs(
    x=NULL, y=NULL,
    title = &quot;Life expectancy, 2015&quot;,
    subtitle = &quot;Shown is period life expectancy at birth. This corresponds to an estimate of the average number\nof years a newborn infant would live if prevailing patterns of mortality at the time of its birth were to\nstay the same throughout its life&quot;,
    caption = &quot;Data source: </code></pre>