Using plotly to visualize political science data

Jon Kropko
February 13, 2017

Loading packages

First I load the needed libraries:

library(dplyr)
library(tidyr)
library(plotly)

State legislature data

Next I load the data that contains information about the party and ideological composition of state legislatures, and state level GDP.

First I collpase the data by year in order to create some interesting plotly graphs.

stleg_gdp <- read.csv("stleg_gdp.csv")
stleg_gdp_year <- group_by(stleg_gdp, year)
stleg_gdp_year <- summarize(stleg_gdp_year, housemean=mean(hou_chamber, na.rm=TRUE),
                            senmean=mean(sen_chamber, 
                                         na.rm=TRUE), 
                            gdpmean=mean(GDPtotal, 
                                         na.rm=TRUE),
                            pcmean=mean(GDPpc, 
                                        na.rm=TRUE),
                            percentmean=mean(private_percent, 
                                             na.rm=TRUE))

State legislature data

Next I collpase the data by state.

stleg_gdp_state <- group_by(stleg_gdp, statename)
stleg_gdp_state <- summarize(stleg_gdp_state, housemean=mean(hou_chamber, na.rm=TRUE),
                            senmean=mean(sen_chamber, 
                                         na.rm=TRUE), 
                            gdpmean=mean(GDPtotal, 
                                         na.rm=TRUE),
                            pcmean=mean(GDPpc, 
                                        na.rm=TRUE), 
                            percentmean=mean(private_percent,
                                             na.rm=TRUE))
vadata <- filter(stleg_gdp, st=="VA")
newdata <- filter(stleg_gdp, st=="VA"|st=="NC"|st=="MD"|st=="PA")
stleg_gdp2 <- stleg_gdp
stleg_gdp2$year <- as.numeric(as.character(stleg_gdp2$year))

State legislature data

Finally I pull out only the data for Virginia, North Carolina, Maryland, and Pennslyvania.

vadata <- filter(stleg_gdp, st=="VA")
newdata <- filter(stleg_gdp, st=="VA"|st=="NC"|st=="MD"|st=="PA")
stleg_gdp2 <- stleg_gdp
stleg_gdp2$year <- as.numeric(as.character(stleg_gdp2$year))

Now we are ready to do some plotting.

Scatterplots

A basic scatterplot:

p <- plot_ly(stleg_gdp_state, x = ~pcmean, y = ~housemean, type = "scatter")
htmlwidgets::saveWidget(as.widget(p), file = "demo1.html")

Scatterplots

A scatterplot with color variant:

p <- plot_ly(stleg_gdp_state, x = ~pcmean, y = ~housemean, type = "scatter", color = ~percentmean)
htmlwidgets::saveWidget(as.widget(p), file = "demo2.html")

Scatterplots

A 3D scatterplot:

p <- plot_ly(stleg_gdp2, x = ~year, y = ~GDPpc, z = ~hou_chamber,
        type = "scatter3d", color = ~private_percent)
htmlwidgets::saveWidget(as.widget(p), file = "demo3.html")

Time series line plots

The left/right ideology of Virginia's state house over time:

p <- plot_ly(vadata, x = ~year, y = ~hou_chamber, type = "scatter", mode = "lines")
htmlwidgets::saveWidget(as.widget(p), file = "demo4.html")

Time series line plots

Comparing Virginia to other states:

p <- plot_ly(newdata, x = ~year, y = ~hou_chamber, color = ~st, type = "scatter", mode = "lines")
htmlwidgets::saveWidget(as.widget(p), file = "demo5.html")