This file contains 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
fp = open("file.txt") | |
data = fp.read().decode("utf-8-sig").encode("utf-8") |
This file contains 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
p1 <- pw + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() + | |
stat_smooth(method='lm') | |
p2 <- px + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() + | |
stat_smooth(method='lm') | |
p3 <- pz + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() + | |
stat_smooth(method='lm') | |
grid.arrange(p1, p2, p3, ncol=1) |
This file contains 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
# create new MarinerNames field that contains only the name of Mariners | |
# players (plagarized from Winston Chang's R Graphics Cookbook Recipe 5.11) | |
outfield$MarinerNames = outfield$Name | |
idx = (outfield$Team.x == "Mariners") | |
outfield$MarinerNames[!idx] = NA | |
# create a new table, taking a subset that has only the Mariners players | |
Mariners = subset(outfield, Team.x == "Mariners") | |
# add the names of the UZR stars to outfield$Table2 sort the table by | |
# wRAA, then add the names of the top 4 wRAA stars | |
outfield$wRAAstars = outfield$Name |
This file contains 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
dat <- data.frame(x = rnorm(10), y = rnorm(10), label = letters[1:10]) | |
#Create a subset of data that you want to label. Here we label points a - e | |
labeled.dat <- dat[dat$label %in% letters[1:5] ,] | |
ggplot(dat, aes(x,y)) + geom_point() + | |
geom_text(data = labeled.dat, aes(x,y, label = label), hjust = 2) | |
#Or add a separate layer for each point you want to label. | |
ggplot(dat, aes(x,y)) + geom_point() + |
This file contains 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
# Recently I wanted to recreate assocplot using ggplot2. In the end I propose a simple way to visualize data arranged two-way tables using geom_tile. | |
# | |
# I used Titanic data set as an example combining age and sex dimensions to get two-way data. | |
# | |
# I plot residuals of Chi-squared test (like in assocplot) on the left and probability of survival on the right. A nice feature of geom_tile is that nicely highlights missing data (children were not crew members). Here is a code generating the plots: | |
library(ggplot2) | |
library(grid) | |
library(reshape2) |
This file contains 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
colSums( x, na.rm= FALSE, dims= 1) | |
rowSums( x, na.rm= FALSE, dims= 1) | |
colMeans(x, na.rm= FALSE, dims= 1) | |
rowMeans(x, na.rm= FALSE, dims= 1) | |
rowsum(x, group, reorder= TRUE) # finds row sums for each level of a grouping variable | |
apply(X, MARGIN, FUN, ...) # applies the function (FUN) to either rows (1) or columns (2) on object X | |
apply(x, 1, min) # finds the minimum for each row | |
apply(x, 2, max) # finds the maximum for each column | |
col.max(x) # another way to find which column has the maximum value for each row | |
which.min(x) |
This file contains 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
install.packages("Hmisc",dependencies=TRUE) | |
update.packages() | |
q() | |
setwd() | |
getwd() | |
ls() # lists objects | |
rm(object) # deletes an object | |
library(RColorBrewer) # load ColorBrewer palettes |
This file contains 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
# | |
# Steve Pittard - [email protected], 03/19/12 | |
# Code to illustrate motivations for using apply function | |
# | |
# See www.bimcore.emory.edu/bbseries for slides and code downloads | |
# | |
# References include: | |
# http://statland.org/R/R/Rpulse2.htm , http://www.cyclismo.org/tutorial/R/tables.html#manipulations | |
# http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/ | |
# |
This file contains 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
# run a quick demonstration | |
install.packages("Hmisc",dependencies=TRUE) | |
update.packages() | |
library("Hmisc") | |
mydata<-spss.get("http://www.rci.rutgers.edu/~rwomack/R/vermont.sav",use.value.labels=TRUE) | |
head(mydata) | |
tail(mydata) | |
summary(mydata) | |
attach(mydata) | |
hist(AGEP) |
This file contains 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
library(lattice) | |
my.wines <- read.csv("http://www.bimcore.emory.edu/wine.csv", header=TRUE) | |
# Look at the correlations | |
library(gclus) | |
my.abs <- abs(cor(my.wines)) | |
my.colors <- dmat.color(my.abs) | |
my.ordered <- order.single(cor(my.wines)) |