Created
March 1, 2019 01:28
-
-
Save k5cents/81f7cd1d4fae5b1fb3a0ded97876f0ec to your computer and use it in GitHub Desktop.
A custom ggplot2 theme to mimick the FiveThirtyEight graphics style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
theme_538 <- function() { | |
theme( | |
plot.title = element_text(face = "bold", | |
family = "Helvetica", | |
size = 18, | |
color = "#222222", | |
lineheight = 0.5, | |
vjust = 1), | |
plot.subtitle = element_text(family = "Helvetica", | |
size = 16, | |
margin = margin(9, 0, 9, 0), | |
color = "#222222", | |
lineheight = 1, | |
vjust = 4), | |
plot.caption = element_text(family = "mono", | |
size = 12, | |
color = "#999999"), | |
panel.background = element_blank(), | |
plot.background = element_rect(fill = "#F0F0F0"), | |
panel.grid.major = element_line(colour = "#cdcdcd", | |
size = 0.75), | |
panel.grid.minor = element_line(size = 0), | |
axis.ticks = element_blank(), | |
axis.text = element_text(color = "#999999", | |
family = "mono", | |
face = "bold", | |
size = 12), | |
axis.title.y = element_text(face = "bold", | |
size = 12, | |
color = "#222222", | |
vjust = 3), | |
axis.title.x = element_text(face = "bold", | |
size = 12, | |
color = "#222222", | |
vjust = -1), | |
plot.margin = unit(c(0.5, 1, 0.5, 0.75), "cm") | |
) | |
} | |
df2 <- tibble(x = 1:10000, | |
y = rbeta(10000, 1, 7)) | |
ggplot(data = df2) + | |
geom_histogram(mapping = aes(x = y), | |
fill = "#ed713a", | |
bins = 75) + | |
labs(title = "These random values have a slight left skew", | |
x = "Random Value", | |
y = "Number of Values", | |
subtitle = | |
paste("Values from 0 to 1, generated randomly from a Beta distribution", | |
"\n", "with parameters 2 and 5"), | |
caption = "SOURCE: rnorm(1000)") + | |
geom_hline(yintercept = 0, size = 0.5, color = "#222222") + | |
geom_vline(xintercept = mean(df2$y), size = 0.5, color = "#222222") + | |
geom_text(aes(label = "Mean of random values", | |
x = 0.25, | |
y = 350), | |
size = 5) + | |
theme_538() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment