Skip to content

Instantly share code, notes, and snippets.

@kjhealy
Last active August 8, 2020 19:35
Show Gist options
  • Save kjhealy/8529dc031b2c88eb3423f50be7f46f0b to your computer and use it in GitHub Desktop.
Save kjhealy/8529dc031b2c88eb3423f50be7f46f0b to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gapminder)
library(ggalt) ## install with install.packages("ggalt", repos = "http://cran.rstudio.com")
## Make some sample data with three countries that each has nine measures
example_wide <- gapminder %>%
select(country, year, lifeExp, pop, gdpPercap) %>%
filter(country %in% c("United States", "Canada", "Mexico"),
year %in% c(1957, 1977, 2007)) %>%
pivot_wider(names_from = year, values_from = lifeExp:gdpPercap)
example_long <- example_wide %>%
pivot_longer(lifeExp_1957:gdpPercap_2007,
names_to = "measure",
values_to = "score")
example_long
## Now plot it in a small multiple
ggplot(data = example_long,
mapping = aes(x = reorder(country, score),
y = score,
color = country)) +
ggalt::geom_lollipop(size = 1) +
guides(color = FALSE) +
scale_y_continuous(labels = scales::label_number_si()) + ## Fancy labels
coord_flip() + ## Actually necessary for geom_lollipop()
labs(x = NULL, y = "Score") +
facet_wrap(~ reorder(measure, score), ncol = 3, scales = "free_x") +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment