This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################################################### | |
# 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) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |