Skip to content

Instantly share code, notes, and snippets.

@shippy
Created June 16, 2016 20:58
Show Gist options
  • Save shippy/1ce4a2881fb950b7267b125167075b5e to your computer and use it in GitHub Desktop.
Save shippy/1ce4a2881fb950b7267b125167075b5e to your computer and use it in GitHub Desktop.
## Dependencies (get them with `install.packages(c("dplyr", "ggplot2", "ggthemes"))`)
library(dplyr)
library(ggplot2)
library(ggthemes)
options(repr.plot.width = 8, repr.plot.height = 5) # Default figure size
## Load + process data
x <- read.csv("gelman_cup_graphic_reporting_challenge_data.csv")
x <- na.omit(x)
# More verbose labels, reverse level order in AGW
x_verbose <- mutate(x, relig = factor(relig, labels = c("Low religiosity", "High religiosity")),
AGW = factor(AGW, levels = c(1, 0), labels = c("Agree", "Disagree")))
# Pithier labels, preserve level order in AGW
x <- mutate(x, relig = factor(relig, labels = c("Low", "High")),
AGW = factor(AGW, labels = c("Disagree", "Agree")))
## 1. Recreation of the original scatterplot with smoothed loess lines
g <- ggplot(x, aes(x = left_right, y = AGW, color = relig)) +
geom_jitter(height = 0.5, width = 0.3, alpha = .7) + geom_smooth(aes(group = relig)) +
xlab("Individual political outlook") + ylab("Answer to statement") +
ggtitle(paste("There is \"solid evidence\" of recent global warming due",
"\"mostly\" to \"human activity such as burning fossil fuels.\"", sep = "\n")) +
theme_bw(base_size = 16) + theme(axis.title.y = element_blank(), axis.ticks = element_blank()) +
scale_color_discrete(name = "Religiosity") +
scale_x_continuous(breaks = c(-1.7, 0, 1.53), # Custom breaks to align with the graph
labels = c(expression(paste(symbol('\254'), " Liberal")), # Pure R can't Unicode
"Moderate",
expression(paste("Conservative ", symbol('\256')))))
g
## 2. Clearer recreation of Anoneuoid's Entry #1
# Common graph basis for this graph and the next
g_hist <- ggplot(x_verbose, aes(x = left_right)) + xlab("Individual politics") +
scale_x_continuous(breaks = c(-1.35, 0, 1.1),
labels = c(expression(paste(symbol('\254'), " Liberal")),
"",
expression(paste("Conservative ", symbol('\256'))))) +
ggtitle(paste0("There is \"solid evidence\" of recent global warming due\n",
"\"mostly\" to \"human activity such as burning fossil fuels.\"")) +
theme_bw(base_size = 16) + theme(axis.text.y = element_blank(), axis.ticks = element_blank())
# Faceted histogram ala Anoneuoid's Entry #1
g_histfacet <- g_hist + # Using the base above
geom_histogram(aes(y = ..density..), binwidth = .4) + facet_grid(relig ~ AGW) +
ylab("Answer count")
g_histfacet
## 3. Clearer comparison of relative frequencies within religiosity / political spectrum (variant of @JoeHilgard)
g_dens <- g_hist + # Using the base from earlier
geom_density(aes(color = AGW, fill = AGW), alpha = .6) + facet_grid(. ~ relig) +
ylab("Answer frequency") +
scale_fill_discrete(name = "", labels = c("Agree ", "Disagree")) + scale_color_discrete(guide = FALSE) +
theme(axis.title.x = element_blank(), legend.position = "top")
g_dens
## Save the above
ggsave("scatterplot.png", plot = g, width = 8, height = 5)
ggsave("histfacet.png", plot = g_histfacet, width = 8, height = 5)
ggsave("density.png", plot = g_dens, width = 8, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment