Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Last active July 13, 2017 16:20
Show Gist options
  • Select an option

  • Save carlislerainey/2f4d693c048cfc7a462b291cf848fb91 to your computer and use it in GitHub Desktop.

Select an option

Save carlislerainey/2f4d693c048cfc7a462b291cf848fb91 to your computer and use it in GitHub Desktop.
Create the plots to motivate Writing Assignment 1 for POLS 209 (Summer 2017).
# remember to set working directory!
# load packages
library(ggplot2)
library(dplyr)
# load data
d <- readRDS("data/state-legislators.rds")
# quick look at data
tibble::glimpse(d)
# remove independents (not a lot of useful information here)
d <- subset(d, party != "Independent")
# calculate the average and SD within each party, state, and year
smry <- summarize(group_by(d, party, state, year),
average = mean(ideology_score),
sd = sd(ideology_score))
# plot the average for each state over time
ggplot(smry, aes(x = year, y = average, color = party)) +
geom_line() +
facet_wrap(~ state) +
labs(x = "Year",
y = "Average Ideology Score",
color = "Party") +
theme_bw() +
scale_color_manual(values = c("blue", "red")) # improve coloring of parties
# plot the SD for each state over time
ggplot(smry, aes(x = year, y = sd, color = party)) +
geom_line() +
facet_wrap(~ state) +
labs(x = "Year",
y = "SD of Ideology Scores",
color = "Party") +
theme_bw() +
scale_color_manual(values = c("blue", "red")) # improve coloring of parties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment