Skip to content

Instantly share code, notes, and snippets.

@cdesante
Created October 21, 2012 20:12
Show Gist options
  • Save cdesante/3928349 to your computer and use it in GitHub Desktop.
Save cdesante/3928349 to your computer and use it in GitHub Desktop.
Basic Melting/Casting
library(ggplot2)
library(foreign)
library(reshape)
ANES <- read.dta("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANESdemo.dta")
#Add meaningful labels for Religion
ANES$religion[ANES$religion==1] <- "Protestant"
ANES$religion[ANES$religion==2] <- "Catholic"
ANES$religion[ANES$religion==3] <- "Jewish"
ANES$religion[ANES$religion==4] <- "Other"
head(ANES)
ANES <- ANES[ANES$year>1960,]
#Truncate to years after 1960
ANES$caseid <- 1:dim(ANES)[1]
head(ANES)
MELTED <- melt(ANES, id=c("caseid","year", "female", "religion", "south"), na.rm=TRUE)
head(MELTED)
PLOTS <- (cast(MELTED, year+south~variable, mean, na.rm=T) )
head(PLOTS)
p1 <- ggplot(data = PLOTS) + geom_point(aes(x = year, y = pid7, colour=south))
p1
FEM <- (cast(MELTED, year+female~variable, mean, na.rm=T) )
head(FEM)
p2 <- ggplot(data = FEM) + geom_point(aes(x = year, y = pid7, colour=female))
p2
RELxFEM <- (cast(MELTED, year+female+religion~variable, mean, na.rm=T) )
head(RELxFEM)
RELxFEM <- RELxFEM[is.na(RELxFEM$religion)==FALSE,]
p3 <- ggplot(data=RELxFEM) + geom_point(aes(x=year, y=ideo7, colour=female)) +coord_cartesian( ylim=c(0,6), xlim=c(1970, 2010)) + facet_wrap(~religion)
p3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment