knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Usage

library(worldtilegrid)
library(ggplot2)

Example (All countries are in the data set)

Ideally you'll always have a complete set of countries. You can use the built-in wtg data set to double check your own data set. It has the following structure:

str(wtg)
str(wtg, give.attr = FALSE)

The x and y variables are the coordinates used by the underlying call(s) to geom_tile().

We'll make some synthetic data and perform a basic plot. The only requirements for geom_wtg() are a column to bind to the country aesthetic and a value to bind to the fill aeshetic. The package takes care of positioning.

set.seed(1)
data.frame(
  ctry = worldtilegrid::wtg$alpha.3,
  val = sample(1000, length(worldtilegrid::wtg$alpha.3)),
  stringsAsFactors = FALSE
) -> xdf

ggplot(xdf, aes(country = ctry, fill = val)) +
  geom_wtg() +
  geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label
  coord_equal() +
  viridis::scale_fill_viridis(direction = -1) +
  labs(title = "World Tile Grid") +
  theme_minimal() +
  theme_enhance_wtg()

Note that you can use theme_enhance_wtg() with any theme since all it does is cruft removal.

Also note that to add labels to the tiles, you must use stat_wtg() so geom_text() has access to the x and y aesthetics as well as the alpha.2 and alpha.3 information (if you're going to use one of those for labels).

Adding labels to the tiles is fraught with peril. Not doing so may be confusing for the vast majority fo the planet who are bereft of geographical knowledge. Doing so may make the colors you used hard to read or (worse) skew the color to an unintended shade. Adding labels to faceted world tile grid maps may make also them unreadable.

Example (Only a few countries are in the data set)

This is far from ideal since there are so few countries, but can come in handy if you're following a measure over time and some countries either did not exist at that time or had no measure for a given time period.

set.seed(1)
data.frame(
  ctry = worldtilegrid::wtg$alpha.3[1:30],
  val = sample(1000, length(worldtilegrid::wtg$alpha.3[1:30])),
  stringsAsFactors = FALSE
) -> xdf

ggplot(xdf, aes(country = ctry, fill = val)) +
  geom_wtg() +
  geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label
  coord_equal() +
  viridis::scale_fill_viridis(direction = -1) +
  labs(title = "World Tile Grid") +
  theme_minimal() +
  theme_enhance_wtg()

Facet Example (All countries are in the data set)

Faceting works as normal since we're doing nothing very special. Consider readability when faceting these since individual world tile grids are already compact and reducing the tile sizes may end up losing the meaning of the fill colors.

set.seed(1)
data.frame(
  ctry = worldtilegrid::wtg$alpha.3,
  val = sample(1000, length(worldtilegrid::wtg$alpha.3)),
  grp = 'Thing One',
  stringsAsFactors = FALSE
) -> xdf1

data.frame(
  ctry = worldtilegrid::wtg$alpha.3,
  val = sample(1000, length(worldtilegrid::wtg$alpha.3)),
  grp = 'Thing Two',
  stringsAsFactors = FALSE
) -> xdf2

rbind.data.frame(
  xdf1,
  xdf2
) -> xdf

ggplot(xdf, aes(country = ctry, fill = val)) +
  geom_wtg() +
  coord_equal() +
  facet_wrap(~grp) +
  viridis::scale_fill_viridis(direction = -1) +
  labs(title = "World Tile Grid Facets") +
  theme_minimal() +
  theme_enhance_wtg()

Facet Example (Only a few countries are in the data set)

The geom will fill in the gaps for you:

set.seed(1)
data.frame(
  ctry = c("USA", "MEX", "CAN", "RUS", "BRA"),
  val = sample(1000, length(c("USA", "MEX", "CAN", "RUS", "BRA"))),
  grp = 'Thing One',
  stringsAsFactors = FALSE
) -> xdf1

data.frame(
  ctry = c("USA", "MEX", "CAN", "RUS", "BRA"),
  val = sample(1000, length(c("USA", "MEX", "CAN", "RUS", "BRA"))),
  grp = 'Thing Two',
  stringsAsFactors = FALSE
) -> xdf2

rbind.data.frame(
  xdf1,
  xdf2
) -> xdf

ggplot(xdf, aes(country = ctry, fill = val)) +
  geom_wtg() +
  coord_equal() +
  facet_wrap(~grp) +
  viridis::scale_fill_viridis(direction = -1) +
  labs(title = "World Tile Grid Facets") +
  theme_minimal() +
  theme_enhance_wtg()


hrbrmstr/worldtilegrid documentation built on July 18, 2019, 12:07 a.m.