Skip to content

Instantly share code, notes, and snippets.

@dggoldst
Last active August 29, 2015 14:23
Show Gist options
  • Save dggoldst/6f3494e68d74cd76c160 to your computer and use it in GitHub Desktop.
Save dggoldst/6f3494e68d74cd76c160 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(dplyr)
#Effective date for same-sex marraige from
#https://en.wikipedia.org/wiki/Same-sex_marriage_in_the_United_States
#with June 2015 for states that had not approved by that date
#Republican two-party vote share in the 2012 election from
#usaelectionatlas.org h/t David Rothschild
df=structure(list(state = structure(1:50, .Label = c("Alabama",
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois",
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
"New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming"), class = "factor"), state_abbrev = structure(c(2L,
1L, 4L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L, 15L, 12L,
16L, 17L, 18L, 21L, 20L, 19L, 22L, 23L, 25L, 24L, 26L, 29L, 33L,
30L, 31L, 32L, 34L, 27L, 28L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 46L, 45L, 47L, 49L, 48L, 50L), .Label = c("AK",
"AL", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "IA",
"ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN",
"MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY",
"OH", "OK", "OR", "PE", "RI", "SC", "SD", "TN", "TX", "UT", "VA",
"VT", "WA", "WI", "WV", "WY"), class = "factor"), republican_vote = c(61.2,
57.3, 54.6, 62.2, 38.1, 47.3, 41.2, 40.6, 49.6, 54, 28.3, 66.4,
41.4, 55.2, 47, 61.1, 61.5, 58.7, 42.1, 36.7, 38.2, 45.2, 46.1,
55.8, 54.8, 57, 61.1, 46.6, 47.2, 41, 44.7, 35.7, 51, 60.1, 48.5,
66.8, 43.7, 47.3, 36, 55.3, 59.2, 60.4, 58, 74.6, 31.8, 48, 42.4,
63.7, 46.5, 71.2), month_effective = c(6L, 10L, 10L, 6L, 6L,
10L, 11L, 7L, 1L, 6L, 12L, 10L, 6L, 6L, 4L, 6L, 6L, 6L, 12L,
1L, 5L, 6L, 8L, 6L, 6L, 11L, 6L, 6L, 1L, 10L, 12L, 7L, 10L, 6L,
6L, 10L, 5L, 4L, 8L, 11L, 6L, 6L, 6L, 10L, 9L, 10L, 12L, 10L,
10L, 10L), year_effective = c(2015L, 2014L, 2014L, 2015L, 2008L,
2014L, 2008L, 2013L, 2015L, 2015L, 2013L, 2014L, 2014L, 2014L,
2009L, 2015L, 2015L, 2015L, 2012L, 2013L, 2004L, 2015L, 2013L,
2015L, 2015L, 2014L, 2015L, 2015L, 2010L, 2013L, 2013L, 2011L,
2014L, 2015L, 2015L, 2014L, 2014L, 2014L, 2013L, 2014L, 2015L,
2015L, 2015L, 2014L, 2009L, 2014L, 2012L, 2014L, 2014L, 2014L
), effective_date = c(2015.46, 2014.79, 2014.79, 2015.46, 2008.46,
2014.79, 2008.88, 2013.54, 2015.04, 2015.46, 2013.96, 2014.79,
2014.46, 2014.46, 2009.29, 2015.46, 2015.46, 2015.46, 2012.96,
2013.04, 2004.38, 2015.46, 2013.62, 2015.46, 2015.46, 2014.88,
2015.46, 2015.46, 2010.04, 2013.79, 2013.96, 2011.54, 2014.79,
2015.46, 2015.46, 2014.79, 2014.38, 2014.29, 2013.62, 2014.88,
2015.46, 2015.46, 2015.46, 2014.79, 2009.71, 2014.79, 2012.96,
2014.79, 2014.79, 2014.79)), .Names = c("state", "state_abbrev",
"republican_vote", "month_effective", "year_effective", "effective_date"
), row.names = c(NA, -50L), class = "data.frame")
p=ggplot(df,aes(x=effective_date,y=republican_vote))
p=p+coord_cartesian(xlim=c(2004,2016))
p=p+scale_x_continuous(breaks=seq(2004,2016,by=2))
p=p+geom_text(aes(label=state_abbrev),jitter=TRUE)
p=p+labs(x="Date",y="Republican Vote Share\n(2012 Presidential Two-Party Share)",title="Date Same-Sex Marriage Law Effective\n(or June 26, 2015)")
p=p + geom_hline(yintercept=50)
p=p+geom_smooth(method="loess",span=1.05,se=FALSE)
p
#Colorful Ribbon plot, contributed by David Lallemant in the comments
m=lm(republican_vote~poly(effective_date,3),df)
plotyears=seq(2004,2016,by=0.1)
p=predict(m,newdata=data.frame(effective_date=plotyears))
df2=data.frame(x=plotyears,y=p)
myalpha=0.7
p2=ggplot(df2,aes(x=x,y=y))
p2=p2+geom_ribbon(aes(ymin=y-8,ymax=y-6, x=x), fill="violet",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y-6,ymax=y-4, x=x), fill="blue",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y-4,ymax=y-2, x=x), fill="turquoise",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y-2,ymax=y-0, x=x), fill="green",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y,ymax=y+2, x=x), fill="yellow",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y+2,ymax=y+4, x=x), fill="orange",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y+4,ymax=y+6, x=x), fill="red",alpha=myalpha,colour=NA)+
geom_ribbon(aes(ymin=y+6,ymax=y+8, x=x), fill="pink",alpha=myalpha,colour=NA)+
theme_bw()+
geom_text(aes(label=state_abbrev, x=effective_date,y=republican_vote),
jitter=TRUE, data = df)+
labs(x="Date",y="Republican Vote Share",title="Date Same-Sex Marriage Law Effective")
p2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment