knitr::opts_chunk$set( collapse = TRUE, comment = "##", fig.path = "figure-markdown_github/")
A simple collection of functions based on the wnominate
package.
library(wnomadds)#devtools::install_github("jaytimm/wnomadds") library(nmlegisdatr)#devtools::install_github("jaytimm/nmlegisdatr")
library(tidyverse) library(wnominate) library(pscl)
datFile <- nmlegisdatr::nml_rollcall %>% filter(Chamber =='House' & !grepl('^LT', Representative)) %>% dplyr::select(Representative, Bill_ID, Rep_Vote) %>% spread(key= Bill_ID, value = Rep_Vote)
datRC <- pscl::rollcall(datFile [,-1], yea='Yea', nay='Nay', missing=c('Excused', 'Absent', 'Rec'), vote.names = colnames(datFile)[2:ncol(datFile)], legis.names = datFile$Representative)
Swap yea/nay to 1, 6, 9
datRC$votes [datRC$votes == 'Excused' | datRC$votes == 'Absent' | datRC$votes == 'Rec'] <- 9 datRC$votes [datRC$votes == 'Yea'] <- 1 datRC$votes [datRC$votes == 'Nay'] <- 6
resultd2 <- wnominate::wnominate (datRC, ubeta=15, uweights=0.5, dims=2, minvotes=20, lop=0.025,trials=3, polarity=c(1,1), verbose=FALSE)
row.names(resultd2$rollcalls) <- colnames(datFile)[2:ncol(datFile)]
Extract legislators from nom
object. And add legislator details from nmlegisdatr
.
house_data <- resultd2$legislators %>% bind_cols(nml_legislators %>% filter(Chamber == 'House' & !grepl('^LT', Representative)))
library(ggthemes) house_data%>% ggplot(aes(x=coord1D, y=coord2D)) + geom_point(aes(color = Party), size= 2.5, shape= 17) + scale_colour_stata() + #theme_fivethirtyeight() + theme(legend.position = 'bottom', plot.title = element_text(size=13), axis.title = element_text(size=10)) + geom_text(aes(label=Representative), size=2.5, check_overlap = TRUE, hjust = 0, nudge_x = 0.03)+ labs(title="New Mexico 53rd House Roll Call - 2nd Session") + coord_equal(ratio=1)
wnomadds
Demonstrate how to extract cutting lines using wnom_adds
.
with_cuts <- wnomadds::wnom_adds_get_cutlines(resultd2, rollcall_obj = datRC) #Suppress warnings
The function returns cutline coordinates along with vote polarity.
head(with_cuts)
Plot legislators with cutting lines.
ggplot () + scale_colour_stata() + theme_fivethirtyeight() + theme(plot.title = element_text(size=13), legend.position = 'bottom') + geom_point(data=house_data, aes(x=coord1D, y=coord2D,color = Party), size= 2.5, shape= 17) + geom_segment(data=with_cuts, aes(x = x_1, y = y_1, xend = x_2, yend = y_2)) + geom_segment(data=with_cuts, aes(x = x_2, y = y_2, xend = x_2a, yend = y_2a), arrow = arrow(length = unit(0.2,"cm"))) + geom_segment(data=with_cuts, aes(x = x_1, y = y_1, xend = x_1a, yend = y_1a), arrow = arrow(length = unit(0.2,"cm")))+ geom_text(data=with_cuts, aes(x = x_1a, y = y_1a, label = Bill_ID), size=2.5, nudge_y = 0.03, check_overlap = TRUE) + coord_fixed(ratio=1) + labs(title="Cutting lines for New Mexico's 53rd Lower Chamber - 2nd Session")
Cutting line selections.
select_cuts <- c('HB0032') sub <- nmlegisdatr::nml_rollcall %>% filter(Bill_ID %in% select_cuts) %>% inner_join(house_data) cut_sub <- subset(with_cuts, Bill_ID %in% select_cuts)
sub %>% ggplot(aes(x=coord1D, y=coord2D)) + geom_point(aes(color = Party_Vote, shape= Party_Vote, fill = Party_Vote), size= 2.5) + nmlegisdatr::nml_color_vote() + nmlegisdatr::nml_fill_vote() + nmlegisdatr::nml_shape_vote()+ theme(legend.position = 'bottom', plot.title = element_text(size=13), axis.title = element_text(size=10)) + geom_text(aes(label=Representative), size=2.5, check_overlap = TRUE, hjust = 0, nudge_x = 0.03)+ geom_segment(data=cut_sub, aes(x = x_1, y = y_1, xend = x_2, yend = y_2)) + geom_segment(data=cut_sub, aes(x = x_2, y = y_2, xend = x_2a, yend = y_2a), arrow = arrow(length = unit(0.2,"cm"))) + geom_segment(data=cut_sub, aes(x = x_1, y = y_1, xend = x_1a, yend = y_1a), arrow = arrow(length = unit(0.2,"cm")))+ geom_text(data=cut_sub, aes(x = x_1a, y = y_1a, label = Bill_ID), size=2.5, nudge_y = 0.03, check_overlap = TRUE) + labs(title="New Mexico 53rd House Roll Call - 2nd Session") + coord_equal(ratio=1)
View multiple roll calls.
select_cuts <- c('HB0032', 'HB0064', 'HB0100', 'HB0049', 'HB0325', 'HJM010', 'HM106', 'SB0079') sub <- nmlegisdatr::nml_rollcall %>% filter(Bill_ID %in% select_cuts) %>% inner_join(house_data) cut_sub <- subset(with_cuts, Bill_ID %in% select_cuts)
sub %>% ggplot(aes(x=coord1D, y=coord2D)) + geom_point(aes(color = Party_Vote, shape= Party_Vote, fill = Party_Vote), size= 1.5) + nmlegisdatr::nml_color_vote() + nmlegisdatr::nml_fill_vote() + nmlegisdatr::nml_shape_vote()+ theme(legend.position = 'bottom', plot.title = element_text(size=13), axis.title = element_text(size=10)) + geom_text(aes(label=Representative), size=1.5, check_overlap = TRUE, hjust = 0, nudge_x = 0.03)+ geom_segment(data=cut_sub, aes(x = x_1, y = y_1, xend = x_2, yend = y_2)) + geom_segment(data=cut_sub, aes(x = x_2, y = y_2, xend = x_2a, yend = y_2a), arrow = arrow(length = unit(0.2,"cm"))) + geom_segment(data=cut_sub, aes(x = x_1, y = y_1, xend = x_1a, yend = y_1a), arrow = arrow(length = unit(0.2,"cm")))+ geom_text(data=cut_sub, aes(x = x_1a, y = y_1a, label = Bill_ID), size=2.5, nudge_y = 0.03, check_overlap = TRUE) + labs(title="New Mexico 53rd House Roll Call - 2nd Session") + coord_equal(ratio=1)+ facet_wrap(~Bill_ID, ncol = 4) #
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.