First I load the needed libraries:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(plotly)
## Warning: package 'plotly' was built under R version 3.3.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.2
##
## 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
Next I load the data that contains information about the party and ideological composition of state legislatures, and state level GDP. I collpase the data by state and 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))
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))
plotlyBasic scatterplot:
plot_ly(stleg_gdp_state, x = ~pcmean, y = ~housemean, type = "scatter")
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: Ignoring 1 observations
Scatterplot with color variant:
plot_ly(stleg_gdp_state, x = ~pcmean, y = ~housemean, type = "scatter", color = ~percentmean)
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: Ignoring 1 observations
3D Scatterplot
plot_ly(stleg_gdp2, x = ~year, y = ~GDPpc, z = ~hou_chamber,
type = "scatter3d", color = ~private_percent)
## No scatter3d mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: Ignoring 69 observations
plot_ly(vadata, x = ~year, y = ~hou_chamber, type = "scatter", mode = "lines")
plot_ly(newdata, x = ~year, y = ~hou_chamber, color = ~st, type = "scatter", mode = "lines")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors