library(tidyverse) library(janitor) library(plyr) library(ggrepel) ### Load Data dw_nom <- read.csv(file = "https://www.christianlindke.com/uploads/2/8/0/0/28002995/hs117_members.csv") ### Align Independents with Party they Coalition With dw_nom <- dw_nom %>% mutate(party_name = case_when(party_code == 200 ~ 'Republican', party_code == 100 ~ 'Democratic', party_code == 328 ~ 'Democratic')) ### Create Plot for the House dw_nom_house <- dw_nom %>% filter(chamber == "House") mean_house <- ddply(dw_nom_house, 'party_name', summarise, grp.mean=mean(nominate_dim1)) mean_house housepol <- ggplot(dw_nom_house, aes(x = nominate_dim1, fill = party_name)) + geom_density(alpha = 0.4) + scale_color_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + scale_fill_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + geom_vline(data = mean_house, aes(xintercept=grp.mean, color=party_name), linetype="dashed") + geom_text(aes(x=-0.392, label="House Democratic Mean -0.392", y=2.5), colour = "black", angle=90) + geom_text(aes(x= 0.503, label="House Republican Mean -0.503", y=2.5), colour="black", angle=90) + geom_histogram(aes(y=after_stat(density)), alpha = 0.5) + labs(fill = "Political Party", color = "Political Party") + xlab("Nominate Score") + ylab("Density") + geom_segment(aes(x = -0.680, y = -0.1, xend = -0.680, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.680, y = -0.2, label = "Jayapal", color = "darkblue") + geom_segment(aes(x = -0.49, y = -0.1, xend = -0.49, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.49, y = -0.2, label = "Pelosi", color = "darkblue") + geom_segment(aes(x = -0.115, y = -0.1, xend = -0.115, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.115, y = -0.2, label = "Golden", color = "purple") + geom_segment(aes(x = 0.157, y = -0.1, xend = 0.157, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.157, y = -0.2, label = "Fitzpatrick", color = "purple") + geom_segment(aes(x = 0.455, y = -0.1, xend = 0.455, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.455, y = -0.2, label = "McCarthy", color = "darkred") + geom_segment(aes(x = 0.765, y = -0.1, xend = 0.765, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.764, y = -0.2, label = "Boebert", color = "darkred") + theme_minimal() housepol ## Create Plot for the Senate dw_nom_senate <- dw_nom %>% filter(chamber == "Senate") mean_senate <- ddply(dw_nom_senate, 'party_name', summarise, grp.mean=mean(nominate_dim1)) mean_senate senatepol <- ggplot(dw_nom_senate, aes(x = nominate_dim1, fill = party_name)) + geom_density(alpha = 0.4) + scale_color_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + scale_fill_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + geom_vline(data = mean_senate, aes(xintercept=grp.mean, color=party_name), linetype="dashed") + geom_text(aes(x=-0.348, label="Senate Democratic Mean -0.392", y=2.5), colour = "black", angle=90) + geom_text(aes(x= 0.524, label="Senate Republican Mean -0.503", y=2.5), colour="black", angle=90) + geom_histogram(aes(y=after_stat(density)), alpha = 0.5) + labs(fill = "Political Party", color = "Political Party") + xlab("Nominate Score") + ylab("Density") + geom_segment(aes(x = -0.753, y = -0.1, xend = -0.753, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.753, y = -0.2, label = "Warrem", color = "darkblue") + geom_segment(aes(x = -0.356, y = -0.1, xend = -0.356, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.356, y = -0.2, label = "Schumer", color = "darkblue") + geom_segment(aes(x = -0.06, y = -0.1, xend = -0.06, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = -0.115, y = -0.2, label = "Manchin", color = "purple") + geom_segment(aes(x = 0.116, y = -0.1, xend = 0.116, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.116, y = -0.2, label = "Collins", color = "purple") + geom_segment(aes(x = 0.405, y = -0.1, xend = 0.405, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.405, y = -0.2, label = "McConnell", color = "darkred") + geom_segment(aes(x = 0.82, y = -0.1, xend = 0.82, yend = 0.1), arrow = arrow(length = unit(0.5, "cm"))) + annotate('text', x = 0.82, y = -0.2, label = "Cruz", color = "darkred") + theme_minimal() senatepol ### Smoothed Density for Both bothpol <- ggplot(dw_nom_senate, aes(x = nominate_dim1, color = party_name)) + geom_density(alpha = 0.4) + scale_color_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + scale_fill_manual(values = c('Republican' = 'red', 'Democratic' = 'blue')) + labs(color = "Political Party Senate") + xlab("Nominate Score") + ylab("Density") + theme_minimal() bothpol bothpol1 <- bothpol + geom_density(data = dw_nom_house, aes(x= nominate_dim1, fill = party_name), alpha = 0.4) + labs(fill = "Political Party House", color = "Political Party") + xlab("Nominate Score") + ylab("Density") + theme_minimal() bothpol1