Tables

Author

Samantha Roloff

The following table is created from the governor_state_toplines_2022 data set available at data/election-forecasts-2022 at master · fivethirtyeight/data (github.com).

What I wanted to convey with this table is the likely winner of the 2022 state governor’s race as of 9/17/2022, how the polling has changed over time, and if the model (expression) used to predict the winner produces different results.

The functionality allows the user to quickly scan and determine which races are very close and then can examine each model to determine why they are producing different results.

In order to format this data set, the data will need to be wrangled in the following ways:

1.) A state, likely winner and likelihood of winning column will be created.

2.) The data will be split by expression type and filtered to the last month of polling.

3.) A sparkline depicting the likely winner’s polling over the last month will be added to the table.

4.) Formatting enhancements to improve readability using the kable and kableExtra packages will be added.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6     ✔ purrr   0.3.4
✔ tibble  3.1.8     ✔ dplyr   1.0.9
✔ tidyr   1.2.0     ✔ stringr 1.4.1
✔ readr   2.1.2     ✔ forcats 0.5.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(dplyr)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(knitr)
library(kableExtra)

Attaching package: 'kableExtra'

The following object is masked from 'package:dplyr':

    group_rows
library(sparkline)

#read in the dataframe
gov_df<-readr::read_csv('governor_state_toplines_2022.csv', show_col_types = FALSE)

#decide if the state is likely to have a democrat or republican governor
gov_df$Governorship_leans <- with(gov_df, ifelse(winner_Rparty < winner_Dparty, 'Democrat', 'Republican'))

# convert date field to date field
gov_df$forecastdate <- as.Date(gov_df$forecastdate, format = "%m/%d/%Y" )
#class(gov_df$forecastdate)

#make a state field
gov_df$state<- strtrim(gov_df$district, 2)

#make a likely winner column for our table
gov_df$likely_winner<-with(gov_df, ifelse(winner_Rparty < winner_Dparty, gov_df$name_D1, gov_df$name_R1))

#likelihood of winning for the likely_winner
gov_df$likelihood_winner <- with(gov_df, ifelse(gov_df$Governorship_leans == 'Democrat', gov_df$winner_D1, gov_df$winner_R1))

#add a blank column for our sparkline to go
gov_df$sparkline<-NA

#filter data to previous month
gov_df <- gov_df %>%
   filter(forecastdate>max(forecastdate)-31)


#reduce the # of columns
gov_df_slim <- subset (gov_df, select = c(state, expression, likely_winner, forecastdate, likelihood_winner, sparkline))

#There is probably a better way to do this, but a nested for loop was taking WAY too long.

#filter to _lite expression
gov_df_lite <- gov_df_slim %>% filter(expression == '_lite')

# add the data for the figure
for(s in gov_df_lite$state)
  {state_polling <- gov_df_lite %>%
    filter(state==s) %>%
    arrange(forecastdate) %>%
    pull(likelihood_winner)
  # add figure to data.frame
  gov_df_lite[which(gov_df_lite$state==s),"sparkline"] <- spk_chr(state_polling)
}

#reduce the # of columns
gov_df_lite <- subset (gov_df_lite, select = c(state, expression, likely_winner, sparkline))

#drop duplicates
gov_df_lite<-distinct(gov_df_lite)



#filter to _classic expression
gov_df_classic <- gov_df_slim %>% filter(expression == '_classic')

# add the data for the figure
for(s in gov_df_classic$state)
  {state_polling <- gov_df_classic %>%
    filter(state==s) %>%
    arrange(forecastdate) %>%
    pull(likelihood_winner)
  # add figure to data.frame
  gov_df_classic[which(gov_df_classic$state==s),"sparkline"] <- spk_chr(state_polling)
}

#reduce the # of columns
gov_df_classic <- subset (gov_df_classic, select = c(state, expression, likely_winner, sparkline))

#drop duplicates
gov_df_classic<-distinct(gov_df_classic)



#filter to _deluxe expression
gov_df_deluxe <- gov_df_slim %>% filter(expression == '_deluxe')

# add the data for the figure
for(s in gov_df_deluxe$state)
  {state_polling <- gov_df_deluxe %>%
    filter(state==s) %>%
    arrange(forecastdate) %>%
    pull(likelihood_winner)
  # add figure to data.frame
  gov_df_deluxe[which(gov_df_deluxe$state==s),"sparkline"] <- spk_chr(state_polling)
}

#reduce the # of columns
gov_df_deluxe <- subset (gov_df_deluxe, select = c(state, expression, likely_winner, sparkline))

#drop duplicates
gov_df_deluxe<-distinct(gov_df_deluxe)


#stack the dataframe
gov_df_slim <- rbind(gov_df_lite, gov_df_classic, gov_df_deluxe)

gov_df_slim <- gov_df_slim %>% arrange(state, expression)

#table formatting
sparkline(0)
tbl_gov_df_slim <- gov_df_slim %>%
  knitr::kable(
    escape=FALSE,
    col.names = c(
    "State",
    "Expression",
    "Likely Winner",
    "Likelihood of Winning"
  )) %>% kable_styling(font_size = 18) %>% 
  kable_classic(full_width = FALSE, html_font = "arial") %>% 
  kable_paper(lightable_options = "hover", full_width = FALSE)


tbl_gov_df_slim
State Expression Likely Winner Likelihood of Winning
AK _classic Mike Dunleavy
AK _deluxe Mike Dunleavy
AK _lite Mike Dunleavy
AL _classic Kay Ivey
AL _deluxe Kay Ivey
AL _lite Kay Ivey
AR _classic Sarah Huckabee Sanders
AR _deluxe Sarah Huckabee Sanders
AR _lite Sarah Huckabee Sanders
AZ _classic Katie Hobbs
AZ _deluxe Katie Hobbs
AZ _lite Katie Hobbs
CA _classic Gavin Newsom
CA _deluxe Gavin Newsom
CA _lite Gavin Newsom
CO _classic Jared Polis
CO _deluxe Jared Polis
CO _lite Jared Polis
CT _classic Edward Lamont
CT _deluxe Edward Lamont
CT _lite Edward Lamont
FL _classic Ron DeSantis
FL _deluxe Ron DeSantis
FL _lite Ron DeSantis
GA _classic Brian Kemp
GA _deluxe Brian Kemp
GA _lite Brian Kemp
HI _classic Josh Green
HI _deluxe Josh Green
HI _lite Josh Green
IA _classic Kim Reynolds
IA _deluxe Kim Reynolds
IA _lite Kim Reynolds
ID _classic Brad Little
ID _deluxe Brad Little
ID _lite Brad Little
IL _classic J.B. Pritzker
IL _deluxe J.B. Pritzker
IL _lite J.B. Pritzker
KS _classic Laura Kelly
KS _deluxe Laura Kelly
KS _lite Laura Kelly
MA _classic Maura Healey
MA _deluxe Maura Healey
MA _lite Maura Healey
MD _classic Wes Moore
MD _deluxe Wes Moore
MD _lite Wes Moore
ME _classic Janet T. Mills
ME _deluxe Janet T. Mills
ME _lite Janet T. Mills
MI _classic Gretchen Whitmer
MI _deluxe Gretchen Whitmer
MI _lite Gretchen Whitmer
MN _classic Tim Walz
MN _deluxe Tim Walz
MN _lite Tim Walz
NE _classic Jim Pillen
NE _deluxe Jim Pillen
NE _lite Jim Pillen
NH _classic Chris Sununu
NH _deluxe Chris Sununu
NH _lite Chris Sununu
NM _classic Michelle Lujan Grisham
NM _deluxe Michelle Lujan Grisham
NM _lite Michelle Lujan Grisham
NV _classic Steve Sisolak
NV _deluxe Steve Sisolak
NV _lite Steve Sisolak
NY _classic Kathy C. Hochul
NY _deluxe Kathy C. Hochul
NY _lite Kathy C. Hochul
OH _classic Mike DeWine
OH _deluxe Mike DeWine
OH _lite Mike DeWine
OK _classic Kevin Stitt
OK _deluxe Kevin Stitt
OK _lite Kevin Stitt
OR _classic Tina Kotek
OR _deluxe Tina Kotek
OR _lite Tina Kotek
PA _classic Josh Shapiro
PA _deluxe Josh Shapiro
PA _lite Josh Shapiro
RI _classic Daniel J. McKee
RI _deluxe Daniel J. McKee
RI _lite Daniel J. McKee
SC _classic Henry McMaster
SC _deluxe Henry McMaster
SC _lite Henry McMaster
SD _classic Kristi Noem
SD _deluxe Kristi Noem
SD _lite Kristi Noem
TN _classic Bill Lee
TN _deluxe Bill Lee
TN _lite Bill Lee
TX _classic Greg Abbott
TX _deluxe Greg Abbott
TX _lite Greg Abbott
VT _classic Phil Scott
VT _deluxe Phil Scott
VT _lite Phil Scott
WI _classic Tony Evers
WI _deluxe Tony Evers
WI _lite Tony Evers
WI _lite Tim Michels
WY _classic Mark Gordon
WY _deluxe Mark Gordon
WY _lite Mark Gordon