Scott Chamberlain
2013-07-30
?plot or ??plot for fuzzy search
or
help("plot")
help(package="ggplot2")
or just execute name of function to see the code
plot
[r]”)Some helpful tips:
def: Integrated development environments
These can make R easier if you are a beginner by bringing all the pieces together (plots, code, help), and autocompleting text for you, etc.
Highly recommend RStudio because
Getting data
Manipulating data
Visualization
Analysis
Writing
read.csv("mycoolfile.csv")
install.packages("gdata")
library(gdata)
read.xls("mycoolfile.xls", sheet="Sheet1")
Um, why would I do this?
Getting data directly in R allows for reproducible workflows = data + analysis + visualizations + writing (hint: see R pkg knitr)
Data/taxonomy/etc. constantly changing = makes sense to query for newest data
rOpenSci at http://ropensci.org/
Definitely learn tools for the split-apply-combine strategy
library(plyr)
head(iris)[, c(1:2, 5)]
Sepal.Length Sepal.Width Species
1 5.1 3.5 setosa
2 4.9 3.0 setosa
3 4.7 3.2 setosa
4 4.6 3.1 setosa
5 5.0 3.6 setosa
6 5.4 3.9 setosa
ddply(iris, .(Species), colwise(mean))[, 1:3]
Species Sepal.Length Sepal.Width
1 setosa 5.006 3.428
2 versicolor 5.936 2.770
3 virginica 6.588 2.974
library(reshape2)
head(iris)[1:3,]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
iris_m <- melt(iris)
head(iris_m)[1:3,1:3]
Species variable value
1 setosa Sepal.Length 5.1
2 setosa Sepal.Length 4.9
3 setosa Sepal.Length 4.7
dcast(iris_m, Species ~ variable)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 50 50 50 50
2 versicolor 50 50 50 50
3 virginica 50 50 50 50
plot(hp ~ mpg, data=mtcars, cex=3, cex.axis=2, cex.lab=2)
library(ggplot2)
ggplot(mtcars, aes(mpg, hp)) +
geom_point(size=4) +
theme_grey(base_size=20)
library(ggplot2)
ggplot(mtcars, aes(mpg, hp, colour=gear)) +
geom_point(size=4) +
facet_wrap(~carb) +
theme_grey(base_size=20)
Way too much to cover here, there can be a lot of intracacies to analyses:
Ask the authors of the packages: email directly, probably slower than below options
R mailing lists, StackOverflow, etc. (see previous slides)
CRAN Taskviews:
Highly recommend learning knitr
knitr: Mix text w/ code = reproducible documents
Bonus: it's integrated in to RStudio
You can combine LaTeX or Markdown with your code
For Word users = try Markdown with knitr first, shallower learning curve relative to LaTeX.
library(knitr) then in Toolbar do New File then R Markdown to get started