# Packages library(sfi) library(webshot) library(ggplot2) library(dplyr) library(plotly) library(ggiraph) library(scales) library(tidyverse) library(directlabels) library(knitr) library(Hmisc) library(gridExtra) library(RColorBrewer) library(extrafont) library(readr) library(kableExtra) library(reshape2) library(grid) # webshot::install_phantomjs() loadfonts() ## Global options options(max.print="75") opts_chunk$set(echo=FALSE, cache=FALSE, prompt=FALSE, tidy=TRUE, comment=NA, message=FALSE, warning=FALSE, dpi = 300, dev = c("png", "cairo_pdf"), fig.pos="!h", fig.path = 'figures/') opts_knit$set(width=75) options(xtable.comment = FALSE)
# Eidelman, Kornilova, Argyle 1 (version 1) # read in stat dict from current directory state_dict <- read_csv('state_dict.csv') # read in data data <- all_data$eidelman$f1 # make states upper case data$State <- toupper(data$State) # join by state data <- inner_join(data, state_dict, by = 'State') data$State <- NULL names(data) <- c('Pass', 'Intro', 'State') # make data long format data <- melt(data, id.vars = 'State') # # g1 <- ggplot(data, # aes(reorder(x = State, -value), # y = value, # fill = variable)) + # scale_fill_manual(name = '', # values = c('black', 'darkgrey')) + # geom_bar(stat = 'identity') + # coord_flip() + # labs(x = '', # y = '', # title = 'Figure 1', # subtitle = 'Number of bills introduced and receiving floor action for each state', # caption = '') + # theme_sfi(lp = 'bottom', # gM = FALSE, # y_axis_title_style = 'bold', # title_style = 'bold', # lkw = TRUE, # lkt = 'point', # legend_width = 40) + # theme(axis.title = element_text(size = 10, hjust = 1), # axis.text = element_text(size = 10, hjust = 1)) # # g1 #
# candlestick flipped g2 <- ggplot(data, aes(State, value, color = variable)) + geom_segment(aes(x=reorder(State, value), xend=State, y=0, yend=value), size = 1, color = 'black', alpha = 0.5) + coord_flip() + geom_point(size= 2, alpha = 1) + scale_color_manual(name = '', values = c('#BFBFBF', '#000000')) + labs(x = '', y = '', title = 'Figure 1', subtitle = paste0('Number of bills introduced and receiving','\n','floor action for each state'), caption = '') + theme_sfi(lp = 'bottom', gM = FALSE, y_axis_title_style = 'bold', title_style = 'bold', lkw = TRUE, lkt = 'point', legend_width = 40) + theme(axis.title = element_text(size = 10, hjust = 1), axis.text = element_text(size = 10, hjust = 1)) g2
# Eidelman, Kornilova, Argyle 2 (version 1) # # no scientific notation # options(scipen = '999') # no_zero_scale <- function(x) paste0(x*100, "%") # read in stat dict from current directory state_dict <- read_csv('state_dict.csv') # # get data data <- all_data$eidelman$f2 # make states upper case data$State <- toupper(data$State) # join data <- inner_join(data, state_dict, by = 'State') data$State <- NULL names(data) <- c('value','State') # barplot # g1 <- ggplot(data, # aes(reorder(x = State, value), # y = value)) + # geom_bar(stat = 'identity', width = 0.9) + # scale_y_continuous(labels = no_zero_scale) + # coord_flip() + # labs(x = '', # y = '', # title = 'Figure 2', # subtitle = 'Percent of bills reaching floor per state', # caption = '') + # theme_sfi(lp = 'bottom', # gM = FALSE, # y_axis_title_style = 'bold', # title_style = 'bold') + # theme(axis.title.x=element_blank(), # axis.text.x=element_blank(), # axis.ticks.x=element_blank()) + # theme(axis.title = element_text(size = 10, hjust = 1), # axis.text = element_text(size = 10, hjust = 1)) # g1
# # get data no_zero_scale <- function(x) paste0(x*100, "%") # candlestick g2 <- ggplot(data, aes(reorder(x = State, value), y = value)) + geom_segment(aes(x=reorder(State, -value), xend=State, y=0, yend=value), size = 0.5, color = 'black', alpha = 0.5) + geom_point(size= 2, alpha = 0.8) + scale_y_continuous(labels = no_zero_scale) + labs(x = '', y = '', title = 'Figure 2', subtitle = 'Percent of bills reaching floor per state', caption = '') + theme_sfi(lp = 'bottom', base_size = 10, gM = FALSE, y_axis_title_style = 'bold', title_style = 'bold') + theme(axis.title.x=element_blank(), axis.ticks.length = unit(0, "lines"), axis.ticks.x=element_blank()) + coord_flip() + theme(axis.title = element_text(size = 10, hjust = 1), axis.text = element_text(size = 10, hjust = 1)) g2
# Eidelman, Kornilova, Argyle 3 (version 1) # # no scientific notation # options(scipen = '999') # # # get data data <- all_data$eidelman$f3 names(data) <- c('State', 'value') data$State <- toupper(data$State) no_zero_scale <- function(x) paste0(x*100, "%") # g1 <- ggplot(data, # aes(reorder(x = State, -value), # y = value)) + # geom_bar(stat = 'identity', width = 0.8) + # scale_y_continuous(labels = no_zero_scale) + # # scale_y_continuous(labels = percent, limits = c(-.25, .21), breaks = c(-.20,-.15, -.10, -.05, .05,.10, .15,.20)) + # labs(x = '', # y = '', # title = 'Figure 3', # subtitle = 'Change from baseline with text only features', # caption = '') + # theme_sfi(lp = 'bottom', # gM = FALSE, # y_axis_title_style = 'bold', # title_style = 'bold')+ # theme(axis.title = element_text(size = 8, hjust = 1), # axis.text.y = element_text(size = 6.25, hjust = 1), # axis.text.x = element_text(size = 8, hjust = 1)) + # coord_flip() + geom_hline(yintercept = 0) # # g1
# # no scientific notation # options(scipen = '999') no_zero_scale <- function(x) paste0(x*100, "%") # # get data data <- all_data$eidelman$f3 names(data) <- c('State', 'value') data$State <- toupper(data$State) # candlestick g2 <- ggplot(data, aes(reorder(x = State, value), y = value)) + geom_segment(aes(x=reorder(State, -value), xend=State, y=0, yend=value), size = 1, color = 'black', alpha = 0.5) + geom_point(size= 2, alpha = 0.9) + scale_y_continuous(labels = no_zero_scale) + labs(x = '', y = '', title = 'Figure 3', subtitle = 'Change from baseline with text only features', caption = '') + theme_sfi(lp = 'bottom', gM = FALSE, y_axis_title_style = 'bold', title_style = 'bold') + theme(axis.title.x=element_blank(), axis.ticks.x=element_blank()) + theme(axis.title = element_text(size = 8, hjust = 1), axis.text.y = element_text(size = 6.25, hjust = 1), axis.text.x = element_text(size = 8, hjust = 1)) + coord_flip() + geom_hline(yintercept = 0) g2
# # Eidelman, Kornilova, Argyle 3 (version 1) no_zero_scale <- function(x) paste0(x*100, "%") # # get data data <- all_data$eidelman$f4 names(data) <- c('State', 'value') data$State <-toupper(data$State) # g1 <- ggplot(data, # aes(reorder(x = State, -value), # y = value)) + # geom_bar(stat = 'identity', width = 0.8) + # scale_y_continuous(labels = no_zero_scale) + # labs(x = '', # y = '', # title = 'Figure 4', # subtitle = paste0('Change from baseline with sponsor only features', '\n', 'Just sponsor improvement per state/chamber'), # caption = '') + # theme_sfi(lp = 'bottom', # gM = FALSE, # y_axis_title_style = 'bold', # title_style = 'bold') + # coord_flip() + # theme(axis.title = element_text(size = 8, hjust = 1), # axis.text.y = element_text(size = 6.25, hjust = 1), # axis.text.x = element_text(size = 8, hjust = 1)) + # geom_hline(yintercept = 0) # g1 #
# # get data data <- all_data$eidelman$f4 names(data) <- c('State', 'value') data$State <- toupper(data$State) # candlestick g2 <- ggplot(data, aes(reorder(x = State, value), y = value)) + geom_segment(aes(x=reorder(State, -value), xend=State, y=0, yend=value), size = 1, color = 'black', alpha = 0.5) + geom_point(size= 2, alpha = 1) + scale_y_continuous(labels = no_zero_scale) + labs(x = '', y = '', title = 'Figure 4', subtitle = paste0('Change from baseline with sponsor only features', '\n', 'Just sponsor improvement per state/chamber'), caption = '') + theme_sfi(lp = 'bottom', gM = FALSE, y_axis_title_style = 'bold', title_style = 'bold') + theme(axis.title.x=element_blank(), axis.ticks.length = unit(0, "lines"), axis.ticks.x=element_blank()) + theme(axis.title = element_text(size = 8, hjust = 1), axis.text.y = element_text(size = 6.25, hjust = 1), axis.text.x = element_text(size = 8, hjust = 1)) + coord_flip() + geom_hline(yintercept = 0) g2
# # get data data <- all_data$eidelman$f5 data$State <- toupper(data$State) # get levels for each state based on the sum of all data points across variable data$all <- data$Baseline + data$Combined temp <- data temp <- temp[order(temp$all, decreasing = TRUE),] state_levels <- as.character(temp$State) rm(temp) data$all <- NULL data <- melt(data, id.vars = 'State') names(data) <- c('State', 'variable','value') data$State <- as.factor(data$State) data$value <- round((data$value*100)) # relevel data data$State <- factor(data$State, levels = state_levels) # plot g2 <- ggplot(data, aes(value, State)) + geom_segment(aes(x= 0, xend=value, y=reorder(State, value), yend=State), size = 1, alpha = 0.5) + geom_point(size= 2, alpha = 1, aes(color = variable)) + scale_color_manual(name = '', values = c('#000000', '#BFBFBF')) + labs(x = '', y = '', title = 'Figure 5', subtitle = 'Number of bills introduced and receiving floor action for each state', caption = '') + theme_sfi(lp = 'bottom', gM = FALSE, y_axis_title_style = 'bold', title_style = 'bold', lkw = TRUE, lkt = 'point', legend_width = 40) + theme(axis.title = element_text(size = 10, hjust = 1), axis.text = element_text(size = 8, hjust = 1)) g2
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.