# Packages library(sfi) library(webshot) library(ggplot2) library(dplyr) library(plotly) library(ggiraph) library(scales) library(tidyverse) library(knitr) library(Hmisc) library(RColorBrewer) library(extrafont) library(kableExtra) library(grid) 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 = "cairo_pdf", fig.width = 7.25, fig.height = 4, dev = c("png", "cairo_pdf"), fig.pos="!h", fig.path = 'figures/') opts_knit$set(width=75) options(xtable.comment = FALSE)
# Get data data <- all_data$alexander$f4 # Flag the groups data$group <- ifelse(data$key %in% c('Exemption', 'Color', 'Race', 'National origin', 'Religion', 'Sex', 'Retaliation'), 'Claim type', 'National origin') # Get percentage data$value <- data$value * 100 # subset to claim type dat_claim <- data[data$group == 'Claim type',] dat_claim$key <- as.factor(dat_claim$key) colors <- make_colors(7, bw = TRUE) colors <- sort(colors, decreasing = T) # colors <- make_colors(7, bw = TRUE) # candle stick plots --------------------------------------------------- # version 1 one ggplot(dat_claim, aes(reorder(key, -value), value)) + ylim(c(-5, 50)) + geom_segment(aes(x=reorder(key, -value), xend=key, y=0, yend=value), linetype = 'dashed', size = 0.5, alpha = 0.5) + geom_point(size= 25, alpha = 1, color = '#5D5D5D') + geom_text(aes(label = paste0(value, '%')), size = 6, color = 'white', alpha = 0.7) + geom_text(aes(label = key), size = 3.5, color = 'black', alpha = 0.7, vjust = -5) + labs(x = '', y = 'Percent', title = 'Version 1') + theme_sfi(lp = 'none', gM = FALSE) + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())
# second one ------------------ ggplot(dat_claim, aes(x=key, y=value)) + ylim(c(-5, 50)) + geom_segment(aes(x=reorder(key, -value), xend=key, y=0, yend=value), linetype = 'dashed', size = 0.7, alpha = 0.5) + geom_point(size= 25, pch = 21, fill = '#383838', stroke = 1, alpha = 1, color = 'black') + geom_text(aes(label = paste0(value, '%')), size = 6, color = 'white', alpha = 1) + labs(x = '', y = 'Percent', title = 'Version 2') + theme_sfi()
# third one ------------------ ggplot(dat_claim, aes(x=key, y=value)) + ylim(c(-5, 50)) + geom_segment(aes(x=reorder(key, -value), xend=key, y=0, yend=value), linetype = 'dashed', size = 0.7, alpha = 0.5) + geom_point(size= 30, pch = 19, stroke = 1, alpha = 1, color = 'darkgrey') + geom_text(aes(label = paste0(value, '%')), size = 6, color = '#323232', alpha = 1) + labs(x = '', y = 'Percent', title = 'Version 3') + theme_sfi()
# fourth one ------------------ size of points based on value ggplot(dat_claim, aes(x=key, y=value)) + ylim(c(-5, 50)) + geom_segment(aes(x=reorder(key, -value), xend=key, y=0, yend=value), linetype = 'dashed', size = 0.7, alpha = 0.5) + geom_point(aes(size = value), pch = 19, stroke = 1, alpha = 1, color = '#505050') + geom_text(aes(label = paste0(value, '%'), color = reorder(key, -value)), size = 4, alpha = 1) + scale_size_area(name = '', max_size = 30) + scale_color_manual(name = '', values= c('white', 'white', 'white', 'white','white','white',' black')) + labs(x = '', y = 'Percent', title = 'Version 4') + theme_sfi(lp = 'none')
# fifth one all on one line ------------------ ggplot(dat_claim, aes(x=group, y=value)) + ylim(c(-5, 55)) + geom_segment(aes(x=group, xend=group, y=0, yend=value), linetype = 1, size = 0.3, alpha = 1) + geom_point(aes(size = value), pch = 21, fill = 'darkgrey', stroke = 1, alpha = 1, color = 'black') + annotate(geom = 'text', label = paste0('Color', '\n', '0%'), x = 1.1, y = 1, size = 3) + annotate(geom = 'text', label = paste0('Religion', '\n', '2%'), x = .88, y = 1.5, size = 3) + annotate(geom = 'text', label = paste0('National origin', '\n', '5%'), x = 1.1, y = 7, size = 3) + annotate(geom = 'text', label = paste0('Exemption', '\n', '5%'), x = .88, y = 7, size = 3) + annotate(geom = 'text', label = paste0('Race', '\n', '24%'), x = 1.115, y = 24, size = 3) + annotate(geom = 'text', label = paste0('Sex', '\n', '26%'), x = .88, y = 26, size = 3) + annotate(geom = 'text', label = paste0('Retaliation', '\n', '38%'), x = 1, y = 48) + scale_size_area(name = '', max_size = 30) + labs(x = '', y = 'Percent', title = 'Version 5') + theme_sfi(lp = 'none')
# get a color vector colors <- make_colors(length(unique(dat_claim$key)), bw = TRUE) # sort data dat_claim <- dat_claim[order(dat_claim$value, decreasing = TRUE),] # make the zero be 0.5 dat_claim$value[dat_claim$key == 'Color'] <- 0.4 # remove dark color # get font object inside_f <- list( family = "CMU Bright", size = 60, color = 'white' ) outside_f <- list( family = "CMU Bright", size = 30, color = colors ) # text font t <- list( family = "CMU Bright", size = 60, color = '#353535' ) # plot plot_ly(dat_claim, labels = ~key, values = ~round(value), type ='pie', hole = 0.4, textposition = 'outside', textinfo = 'percent+label', outsidetextfont = outside_f, marker = list(colors = colors)) %>% config(displayModeBar = F) %>% layout(title ='' , font = t, showlegend = F, annotations = list( showarrow = FALSE, text = paste0('Claim type', '\n', 'Version 8'), font = t), xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
######-------------------------------------------------------- # PLOT 2 # get a color vector colors <- c("#989898", "#989898", "#989898", "#989898", "#989898", "#989898", "#989898") # remove dark color # get font object inside_f <- list( family = "CMU Bright", size = 60, color = 'black' ) # text font t <- list( family = "CMU Bright", size = 60, color = '#353535' ) # plot plot_ly(dat_claim, labels = ~key, values = ~value, type ='pie', hole = 0.4, textposition = 'inside', textinfo = 'percent+label', outsidetextfont = outside_f, marker = list(colors = colors, line = list(color = '#FFFFFF', width = 2))) %>% config(displayModeBar = F) %>% layout(title ='' , font = t, showlegend = F, annotations = list( showarrow = FALSE, text = paste0('Claim type', '\n', 'Version 7'), font = t), xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
######-------------------------------------------------------- # PLOT 3 # get a color vector colors <- make_colors(length(unique(dat_claim$key)), bw = TRUE) # sort data dat_claim <- dat_claim[order(dat_claim$value, decreasing = TRUE),] # remove dark color # get font object inside_f <- list( family = "CMU Bright", size = 60, color = 'white' ) outside_f <- list( family = "CMU Bright", size = 30, color = colors ) # text font t <- list( family = "CMU Bright", size = 60, color = '#353535' ) # plot plot_ly(dat_claim, labels = ~key, values = ~value, type ='pie', hole = 0.6, textposition = 'outside', textinfo = 'percent', outsidetextfont = outside_f, marker = list(colors = colors, line = list(color = '#FFFFFF', width = 3))) %>% add_trace(dat_claim, labels = ~key, values = ~value, type ='pie', hole = 0.6, textposition = 'inside', textinfo = 'label', insidetextfont = inside_f, marker = list(colors = colors, line = list(color = '#FFFFFF', width = 3))) %>% config(displayModeBar = F) %>% layout(title ='' , font = t, showlegend = F, annotations = list( showarrow = FALSE, text = paste0('Claim type', '\n', 'Version 8'), font = t), xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
####### -------------------- make a nice barplot # relevel key dat_claim$key <- as.factor(dat_claim$key) dat_claim$key <- factor(dat_claim$key, levels = c('Retaliation', 'Sex', 'Race', 'Exemption', 'National origin', 'Religion', 'Color')) colors <- c('#000000', '#383838', '#565656', '#868686', '#A2A2A2', '#C2C2C2', '#EAE8E8') ggplot(dat_claim, aes(key, value, fill = key)) + geom_bar(stat= 'identity', color = 'grey', alpha = 0.7) + ylim(c(0, 40)) + xlab('Claim type') + ylab('Percent') + ggtitle('Version 9') + geom_text(aes(label = paste0(value, ' %'), vjust = -0.6), alpha = 0.7) + scale_fill_manual(name = '', values = c('#000000', '#383838', '#565656', '#868686', '#A2A2A2', '#C2C2C2', '#EAE8E8')) + theme_sfi(lp = 'none')
####### -------------------- make a nice barplot # relevel key dat_claim$key <- as.factor(dat_claim$key) dat_claim$key <- factor(dat_claim$key, levels = c('Retaliation', 'Sex', 'Race', 'Exemption', 'National origin', 'Religion', 'Color')) colors <- c('#000000', '#383838', '#565656', '#868686', '#A2A2A2', '#C2C2C2', '#EAE8E8') ggplot(dat_claim, aes(key, value, fill = key)) + geom_bar(stat= 'identity', color = 'grey', alpha = 0.7) + ylim(c(0, 40)) + xlab('Claim type') + ylab('Percent') + ggtitle('Version 10') + coord_flip() + geom_text(aes(label = paste0(value, ' %'), hjust = -0.3), alpha = 0.7) + scale_fill_manual(name = '', values = c('#000000', '#383838', '#565656', '#868686', '#A2A2A2', '#C2C2C2', '#EAE8E8')) + theme_sfi(lp = 'none')
# no scientific notation options(scipen = '999') # get data data <- all_data$livermore$f1 # version 1 ggplot(data, aes(x = median_year, y = friendscr)) + geom_smooth(method = 'lm', linetype = 0, se = TRUE, fill = '#373737', alpha = 0.4) + geom_point(size = 4, alpha = 0.8, pch = 16, color = 'black') + labs(x = '', y = 'Friendliness score', title = 'Version 11', caption = 'Standard errors estimated with a linear regression') + scale_y_continuous(labels = percent, limits = c(-0.018, 0.004), breaks=c(-0.018,-0.016,-0.014,-0.012,-0.01, -0.008, -0.006, -0.004, -0.002, 0, 0.002, 0.004)) + theme_sfi() + geom_text(data=subset(data, justice == 'alito'), aes(median_year, friendscr, label=paste0('Justice ', Hmisc::capitalize(justice))), vjust = 1.5, hjust = 1)
# version 1 ggplot(data, aes(x = median_year, y = friendscr)) + geom_smooth(method = 'lm', linetype = 1, size = 5, se = FALSE, color = 'grey', alpha = 0.4) + geom_point(size = 4, alpha = 0.8, pch = 16, color = 'black') + labs(x = '', y = 'Friendliness score', title = 'Version 12', caption = 'Standard errors estimated with a linear regression') + scale_y_continuous(labels = percent, limits = c(-0.018, 0.004), breaks=c(-0.018,-0.016,-0.014,-0.012,-0.01, -0.008, -0.006, -0.004, -0.002, 0, 0.002, 0.004)) + theme_sfi() + geom_text(data=subset(data, justice == 'alito'), aes(median_year, friendscr, label=paste0('Justice ', Hmisc::capitalize(justice))), vjust = 1.5, hjust = 1)
# version 3 ggplot(data, aes(x = median_year, y = friendscr)) + geom_smooth(method = 'lm', linetype = 0, se = TRUE, fill = '#373737', alpha = 0.3) + geom_point(size = 4, aes(alpha = -friendscr), pch = 16, color = 'black') + scale_alpha(name = '', range = c(0.4, 1)) + labs(x = '', y = 'Friendliness score', title = 'Version 13', caption = 'Standard errors estimated with a linear regression') + scale_y_continuous(labels = percent, limits = c(-0.018, 0.004), breaks=c(-0.018,-0.016,-0.014,-0.012,-0.01, -0.008, -0.006, -0.004, -0.002, 0, 0.002, 0.004)) + theme_sfi(lp = 'none') + geom_text(data=subset(data, justice == 'alito'), aes(median_year, friendscr, label=paste0('Justice ', Hmisc::capitalize(justice))), vjust = 1.5, hjust = 1)
# Version 14 - this is the original, that they liked, but with slightly larger points, as they suggested. # Get data data <- all_data$feldman$f1 # recode federal data$federal <- ifelse(data$federal == '1', 'Federal', 'State') ggplot(data, aes(x = federal, y = clarity_score)) + ylim(c(-3, 11)) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..+..scaled../3.5, yend=..y.., alpha=(..scaled../3)^5), size=3, color = 'darkgrey', trim=TRUE) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..-..scaled../3.5, yend=..y.., alpha=(..scaled../3)^5), size=3, linetype = 1, color = 'darkgrey', trim=TRUE) + labs(x = '', y = 'Clarity Score', title = 'Version 14, spherical cloud represents distrubition') + scale_alpha_continuous(range= c(-0, .5)) + geom_jitter(size = 1, color = 'black', width = 0.3, alpha = 0.3, pch = 16) + theme_sfi(lp = 'none')
# Version 15 ggplot(data, aes(x = federal, y = clarity_score)) + ylim(c(-3, 11)) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..+..scaled../5, yend=..y.., alpha=(..scaled../3)^1), size=3, color = 'darkgrey', trim=TRUE) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..-..scaled../5, yend=..y.., alpha=(..scaled../3)^1), size=3, linetype = 1, color = 'darkgrey', trim=TRUE) + labs(x = '', y = 'Clarity Score', title = 'Version 15, spherical cloud represents distrubition') + scale_alpha_continuous(range= c(-0, .5)) + geom_jitter(size = 1.5, color = 'black', width = 0.2, alpha = 0.3, pch = 16) + theme_sfi(lp = 'none')
# Version 16 ggplot(data, aes(x = federal, y = clarity_score)) + ylim(c(-3, 11)) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..+..scaled../4, yend=..y.., alpha=(..scaled../3)^2), size=3, color = '#838383', trim=TRUE) + stat_ydensity(geom="segment", adjust = 20, scale = 'area', aes(xend=..x..-..scaled../4, yend=..y.., alpha=(..scaled../3)^2), size=3, linetype = 1, color = '#838383', trim=TRUE) + labs(x = '', y = 'Clarity Score', title = 'Version 16, spherical cloud represents distrubition') + scale_alpha_continuous(range= c(-0, .5)) + geom_jitter(size = 2, color = 'black', width = 0.25, alpha = 0.25, pch = 16) + theme_sfi(lp = 'none')
##################################################################################################### # make plot for frankenreiter 5 so that each point colored based on time, and each decade is labeled ##################################################################################################### # Get data data <- all_data$frankenreiter$f5_1 cols <- make_colors(length(unique(data$year)), bw = TRUE) # plot the 1955 to 2014 data ggplot(data, aes(x = coord2, y = coord1, fill = year)) + geom_point(size = 4, pch = 21, alpha = 0.7) + ggrepel::geom_text_repel(data=subset(data, year == 1960 | year == 1970 | year == 1980 | year == 1990 | year == 2000 | year== 2010), aes(coord2, coord1, label=year), vjust = -1.5, hjust = 2.2) + theme_sfi() + labs(x = 'Coordinate 2', y = 'Coordinate 1', title = 'Version 17 (light to dark)') + scale_fill_manual(name = '', values = sort(cols, decreasing = TRUE)) + theme(legend.position = 'none')
# Get data data <- all_data$frankenreiter$f5_1 cols <- make_colors(length(unique(data$year)), bw = TRUE) # plot the 1955 to 2014 data ggplot(data, aes(x = coord2, y = coord1, fill = year)) + geom_point(size = 4, pch = 21, alpha = 0.7) + ggrepel::geom_text_repel(data=subset(data, year == 1960 | year == 1970 | year == 1980 | year == 1990 | year == 2000 | year== 2010), aes(coord2, coord1, label=year), vjust = -1.5, hjust = 2.2) + theme_sfi() + labs(x = 'Coordinate 2', y = 'Coordinate 1', title = 'Version 18 (dark to light)') + scale_fill_manual(name = '', values = sort(cols)) + theme(legend.position = 'none')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.