Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Created July 6, 2015 01:46
Show Gist options
  • Select an option

  • Save carlislerainey/89eaba5fdce38468a554 to your computer and use it in GitHub Desktop.

Select an option

Save carlislerainey/89eaba5fdce38468a554 to your computer and use it in GitHub Desktop.
Plotting uninsurance and health policy attitudes.
# load packages
library(readr) # for reading data
library(ggplot2) # for graphics
library(dplyr) # for data manipulation
library(gridExtra) # for arranging plots in grids
library(texreg) # for printing output
library(arm) # for rescale() function
# load data
health_data <- read_csv("https://raw.githubusercontent.com/carlislerainey/intro-methods/gh-pages/data/health.csv")
# convert ideology to factor
health_data <- mutate(health_data, ideology_cat = cut(ideology,
breaks = c(-Inf, quantile(ideology, c(1/3, 2/3)), Inf),
labels = c("Liberal", "Moderate", "Conservative")))
# plot 1: support expansion
gg1 <- ggplot(health_data, aes(x = percent_uninsured,
y = percent_supporting_expansion)) +
geom_point(method = "rlm") +
geom_smooth(method = "rlm") +
geom_text(aes(label = state_abbr), hjust = -0.3, size = 3) +
labs(x = "Percent Uninsured in 2012",
y = "Percent Supporting Medicaid Expansion in 2012",
title = "Attitudes Toward the Medicaid Expansion") +
theme_bw()
plot(gg)
# plot 2: support aca
gg2 <- gg1 + aes(y = percent_favorable_aca) +
labs(y = "Percent Favorable to ACA in 2012",
title = "Attitudes Toward the ACA")
plot(gg2)
# combine plots
pdf("~/Desktop/health1.pdf", height = 6, width = 15)
grid.arrange(gg2, gg1, ncol = 2)
dev.off()
# break down plot 1 by color
gg3 <- gg1 + aes(color = ideology_cat) +
labs(color = "Ideology") +
theme(legend.position = "bottom")
plot(gg3)
# break down plot 2 by color
gg4 <- gg3 + aes(y = percent_favorable_aca) +
labs(y = "Percent Favorable to ACA in 2012",
title = "Attitudes Toward the ACA")
plot(gg4)
# combine plots
pdf("~/Desktop/health2.pdf", height = 7, width = 15)
grid.arrange(gg4, gg3, ncol = 2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment