Skip to content

Instantly share code, notes, and snippets.

@ericpgreen
Created June 14, 2019 08:33
Show Gist options
  • Select an option

  • Save ericpgreen/bde5097e586e605b5f389cab3d2b3bc3 to your computer and use it in GitHub Desktop.

Select an option

Save ericpgreen/bde5097e586e605b5f389cab3d2b3bc3 to your computer and use it in GitHub Desktop.
Visualizing OWID plot as slope chart
# Visualizing OWID plot as slope chart
# @ericpgreen
# original: https://ourworldindata.org/does-the-news-reflect-what-we-die-from?linkId=68864855
library(tidyverse)
library(ggrepel)
library(viridis)
#library(RColorBrewer)
# get data
# https://ourworldindata.org/does-the-news-reflect-what-we-die-from?linkId=68864855
dat <-
read.csv("https://www.dropbox.com/s/oa7tnndu6jg7b6b/owid-death-search-reporting.csv?dl=1",
stringsAsFactors = FALSE) %>%
filter(year==2016) %>%
select(-year)
# to factor
dat$type <- factor(dat$type,
levels = c("deaths", "google", "nyt", "guardian"),
labels = c("Deaths","Searches","Media: NYT","Media: Guardian"),
ordered = TRUE)
# plot style
# https://ibecav.github.io/slopegraph/
MySpecial <- list(
# move the x axis labels up top
scale_x_discrete(position = "top"),
theme_bw(),
# Format tweaks
# Remove the legend
theme(legend.position = "none"),
# Remove the panel border
theme(panel.border = element_blank()),
# Remove just about everything from the y axis
theme(axis.title.y = element_blank()),
theme(axis.text.y = element_blank()),
theme(panel.grid.major.y = element_blank()),
theme(panel.grid.minor.y = element_blank()),
# Remove a few things from the x axis and increase font size
theme(axis.title.x = element_blank()),
theme(panel.grid.major.x = element_blank()),
theme(axis.text.x.top = element_text(size=12)),
# Remove x & y tick marks
theme(axis.ticks = element_blank()),
# Format title & subtitle
theme(plot.title = element_text(size=20, face = "bold", hjust = 0.5)),
theme(plot.subtitle = element_text(size=16, hjust = 0.5))
)
# plot
# https://ibecav.github.io/slopegraph/
ggplot(data = dat, aes(x = type, y = share, group = cause)) +
geom_line(aes(color = cause, alpha = 1), size = 1) +
scale_color_viridis(discrete = TRUE, option = "C") +
#scale_color_brewer(palette = "RdYlBl") +
geom_text_repel(data = dat %>% filter(type == "Deaths"),
aes(label = cause) ,
hjust = "left",
fontface = "bold",
size = 3,
nudge_x = -.45,
direction = "y") +
geom_text_repel(data = dat %>% filter(type == "Media: Guardian"),
aes(label = cause) ,
hjust = "right",
fontface = "bold",
size = 3,
nudge_x = .5,
direction = "y") +
geom_label(aes(label = share),
size = 2.5,
label.padding = unit(0.05, "lines"),
label.size = 0.0) +
MySpecial +
labs(
title = "Causes of death in the US",
subtitle = "What Americans die from, what they search on Google, and what the media reports on",
caption = "Based on a data visualization from https://ourworldindata.org/does-the-news-reflect-what-we-die-from?linkId=68864855"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment