This polling data is from 538. https://github.com/fivethirtyeight/data/tree/master/election-forecasts-2022.
I’d like to show the current polling for the outcome of the 2022 governorship races across the United States.
The functionality I built in was a hover to be able to hover over individual data points and see the district, date and chance that the Democratic party wins the governorship. Additionally, one is able to doubleclick on a state in the legend to isolate the polling history for just that state.
This graph is reproducible if one downloads the same data set from the link above, albeit with a few more polling dates.
Future improvements could add a zoom in feature, especially to zoom in on the chances close to 50% (0.50), like Arizona and Kansas, where the state moves from Republican to Democratic governor around August, 2022.
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
gov_df<-readr::read_csv('governor_state_toplines_2022.csv', show_col_types = FALSE)
head(gov_df)
## # A tibble: 6 × 87
## cycle branch distr…¹ forec…² expre…³ name_D1 name_D2 name_D3 name_D4 name_I1
## <dbl> <chr> <chr> <chr> <chr> <chr> <lgl> <lgl> <lgl> <chr>
## 1 2022 Governor WY-G1 9/17/2… _lite Theres… NA NA NA Jared …
## 2 2022 Governor WI-G1 9/17/2… _lite Tony E… NA NA NA Joan E…
## 3 2022 Governor VT-G1 9/17/2… _lite Brenda… NA NA NA <NA>
## 4 2022 Governor TX-G1 9/17/2… _lite Beto O… NA NA NA <NA>
## 5 2022 Governor TN-G1 9/17/2… _lite Jason … NA NA NA <NA>
## 6 2022 Governor SD-G1 9/17/2… _lite Jamie … NA NA NA Tracey…
## # … with 77 more variables: name_R1 <chr>, name_R2 <chr>, name_R3 <lgl>,
## # name_R4 <lgl>, name_O1 <chr>, winner_D1 <dbl>, winner_D2 <dbl>,
## # winner_D3 <dbl>, winner_D4 <dbl>, winner_R1 <dbl>, winner_R2 <dbl>,
## # winner_R3 <dbl>, winner_R4 <dbl>, winner_I1 <dbl>, winner_O1 <dbl>,
## # winner_Dparty <dbl>, winner_Rparty <dbl>, tipping <dbl>, vpi <dbl>,
## # mean_predicted_turnout <dbl>, p90_simmed_turnout_gross <dbl>,
## # p10_simmed_turnout_gross <dbl>, voteshare_mean_D1 <dbl>, …
dim(gov_df)
## [1] 10908 87
#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)
#filter to expression field so we don't have duplicate entries/state
gov_df_lite <- filter(gov_df, expression == '_lite')
dim(gov_df_lite)
## [1] 3636 88
head(gov_df_lite)
## # A tibble: 6 × 88
## cycle branch district forecastdate express…¹ name_D1 name_D2 name_D3 name_D4
## <dbl> <chr> <chr> <date> <chr> <chr> <lgl> <lgl> <lgl>
## 1 2022 Governor WY-G1 2022-09-17 _lite Theres… NA NA NA
## 2 2022 Governor WI-G1 2022-09-17 _lite Tony E… NA NA NA
## 3 2022 Governor VT-G1 2022-09-17 _lite Brenda… NA NA NA
## 4 2022 Governor TX-G1 2022-09-17 _lite Beto O… NA NA NA
## 5 2022 Governor TN-G1 2022-09-17 _lite Jason … NA NA NA
## 6 2022 Governor SD-G1 2022-09-17 _lite Jamie … NA NA NA
## # … with 79 more variables: name_I1 <chr>, name_R1 <chr>, name_R2 <chr>,
## # name_R3 <lgl>, name_R4 <lgl>, name_O1 <chr>, winner_D1 <dbl>,
## # winner_D2 <dbl>, winner_D3 <dbl>, winner_D4 <dbl>, winner_R1 <dbl>,
## # winner_R2 <dbl>, winner_R3 <dbl>, winner_R4 <dbl>, winner_I1 <dbl>,
## # winner_O1 <dbl>, winner_Dparty <dbl>, winner_Rparty <dbl>, tipping <dbl>,
## # vpi <dbl>, mean_predicted_turnout <dbl>, p90_simmed_turnout_gross <dbl>,
## # p10_simmed_turnout_gross <dbl>, voteshare_mean_D1 <dbl>, …
#Question: By state, how has the chance of the gov seats changed over time?
plot<-ggplot(data = gov_df_lite, aes(forecastdate, winner_Dparty, color = district))+geom_point()+ggtitle("How likely each state is to have a Democratic governor after the 2022 midterms.")+theme_bw()
ggplotly(plot, tooltip = cbind('district', 'winner_Dparty', 'forecastdate'))