Skip to content

Instantly share code, notes, and snippets.

View cdesante's full-sized avatar

Christopher DeSante cdesante

  • Indiana University
View GitHub Profile
@cdesante
cdesante / independence.r
Created November 7, 2012 22:21
Joint Probability Example
Coin <- c()
Die <- c()
for (i in 1:1000) {
Coin[i] <- sample(c("Heads", "Tails"), 1)
Die[i] <- sample(c(1,2,3,4,5,6), 1)
}
table(Coin, Die)
@cdesante
cdesante / ggd5g2.r
Created November 1, 2012 17:11
Brewer Pallettes
#Load ggplot default data: Diamonds
library(ggplot2)
library(gridExtra)
data(diamonds)
head(diamonds)
diamonds <- diamonds[diamonds$color < "J",]
#http://127.0.0.1:25615/library/ggplot2/html/diamonds.html
B1 <- ggplot(data = diamonds ) + geom_point(aes( x = carat, y = price, color=color)
@cdesante
cdesante / ggd5g1.r
Created November 1, 2012 13:48
gradient colors
#Load ggplot default data: Diamonds
library(ggplot2)
library(gridExtra)
data(diamonds)
head(diamonds)
diamonds <- diamonds[diamonds$color < "J",]
#http://127.0.0.1:25615/library/ggplot2/html/diamonds.html
@cdesante
cdesante / ggd4g1.r
Created October 27, 2012 14:17
Color4
#Load ggplot default data: Diamonds
library(ggplot2)
library(gridExtra)
data(diamonds)
head(diamonds)
diamonds <- diamonds[diamonds$color < "J",]
#http://127.0.0.1:25615/library/ggplot2/html/diamonds.html
#Random Colors
my.colors <- sample(colors(), 7)
@cdesante
cdesante / ggd3g1.r
Created October 23, 2012 19:57
color3
X <- c(1)
Y <- c(1)
DF <- as.data.frame(cbind(X,Y))
c.1 <- ggplot(data = DF) + geom_bar(aes(x = X, y = Y, fill = factor(X))) + coord_flip() + theme(legend.position="none") + labs(x="", y="", title="One Colour") + scale_y_continuous(breaks=NULL, expand=c(0,0)) + scale_x_continuous(breaks=NULL, expand=c(0,0))
X <- c(1:2)
Y <- rep(1, length(X))
DF <- as.data.frame(cbind(X,Y))
@cdesante
cdesante / ggd2g2.r
Created October 23, 2012 18:26
color2
Con1 <- ggplot(data = DF) + geom_pointrange(aes(x = year, y = means,
ymin = lows, ymax = highs, colour=factor(dems)) )
Con1 <- Con1 + ylim(0.5,2.5) + labs(title="geom_pointrange")
Con1
Con2 <- ggplot(data = DF) + geom_errorbar(aes(x = year, y = means,
ymin = lows, ymax = highs, colour=factor(dems), width=.5) )
Con2 <- Con2 + ylim(0.5,2.5) + labs(title="geom_errorbar, width=.5")
Con2
Con3 <- ggplot(data = DF) + geom_errorbar(aes(x = year, y = means,
ymin = lows, ymax = highs, colour=factor(dems), width=.75), lty=2 )
@cdesante
cdesante / ggd2g1.r
Created October 23, 2012 18:11
color1
#######################################################
# Example 4: Casting/Melting with custom functions #
#######################################################
#Let's first define a function, SE, to represent the standard error of a
#variable:
library(gridExtra)
library(ggplot2)
SE <- function(X) {
(sd(X, na.rm=TRUE)/ sqrt(length(X)-1) )
@cdesante
cdesante / ggd1g8.r
Created October 23, 2012 17:42
cast8
#Day 1/10: using Reshape
#Christopher DeSante, Ph.D.
ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
head(ANES)
ANES$caseid <- 1:dim(ANES)[1]
head(ANES)
dim(ANES)
#Melting...
#Essentially reshapes the data into a new DF based on
#whichever variables you specify in the id="" option;
@cdesante
cdesante / ggd1g7.r
Created October 23, 2012 17:35
cast7
head(ANES)
party.gender.region <- ANES[,c(1, 4, 12, 16)]
head(party.gender.region)
#Note the additional changes to our id=():
party.gender.region.year <- melt(party.gender.region,
id=c("year", "female", "south"), na.rm=TRUE)
head(party.gender.region.year)
#Note the additional changes to our formula (below):
party.gender.region.time <- (cast(party.gender.region.year,
year+female+south~variable, mean, na.rm=T) )
@cdesante
cdesante / ggd1g6.r
Created October 23, 2012 17:29
cast6
head(ANES)
party.and.region <- ANES[,c(1, 12, 16)]
head(party.and.region)
#Note the additional changes to our id=():
party.and.region.year <- melt(party.and.region, id=c("year", "south"), na.rm=TRUE)
head(party.and.region.year)
#Note the additional changes to our formula (below):
party.region.time <- (cast(party.and.region.year, year+south~variable,
mean, na.rm=T) )
party.region.time