Etienne Low-Décarie
To view presentations:
search: low-decarie wrangling github
http://low-decarie.github.io/Data_wrangling_and_plotting/#/
To view code that generated presentations:
https://github.com/low-decarie/Data_wrangling_and_plotting
You can look at the .Rpres
files that generated these presentations
You can run the code in these presentation (even all of it, using Chunks>Run All)
There is a folder called ./Data/
that contains data relavant to some of the exercises
object <- function(argument1="value1",
argument2="value2",
argument3="value3")
if(!require(ggplot2)){install.packages("ggplot2")}
require(ggplot2)
A basic scatter plot
qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width)
qplot(data=iris,
x=Species,
y=Sepal.Width)
?qplot
Arguments
x
y
…
data
xlab
ylab
main
qplot(data=iris,
x=Sepal.Length,
xlab="Sepal Width (mm)",
y=Sepal.Width,
ylab="Sepal Length (mm)",
main="Sepal dimensions")
produce a basic plot with built in data
CO2
?CO2
BOD
data()
WARNING: THERE ARE MULTIPLE CO2/co2 datasets (CASE SENSITIVE)
A graphic is made of elements (layers)
Editing an element produces a new graph e.g. just change the coordinate system
plot.object<-qplot()
or
plot.object<-ggplot()
plot.object<-plot.object+layer()
print(plot.object)
basic.plot<-qplot(data=iris,
x=Sepal.Length,
xlab="Sepal Width (mm)",
y=Sepal.Width,
ylab="Sepal Length (mm)",
main="Sepal dimensions")
print(basic.plot)
more powerful, more complicated Note: aes() and geom_point()
basic.plot<- ggplot(data=iris)+
aes(x=Sepal.Length,
xlab="Sepal Width (mm)",
y=Sepal.Width,
ylab="Sepal Length (mm)",
main="Sepal dimensions")+
geom_point()
now required to use stat=“”
basic.plot <- basic.plot+
aes(colour=Species,
shape=Species)
print(basic.plot)
Add a geom (eg. linear smooth)
linear.smooth.plot <- basic.plot+
geom_smooth(method="lm", se=F)
print(linear.smooth.plot)
produce a colorful plot containing linear regressions with built in data
CO2
?CO2
msleep
?msleep
OrchardSprays
data()
CO2.plot<-qplot(data=CO2,
x=conc,
y=uptake,
colour=Treatment)
print(CO2.plot)
plot.object<-plot.object + facet_grid(rows~columns)
CO2.plot<-CO2.plot+facet_grid(.~Type)
print(CO2.plot)
Problems when adding the geom_line
print(CO2.plot+geom_line())
Solution: specify groups
CO2.plot<-CO2.plot+geom_line(aes(group=Plant))
print(CO2.plot)
cheatsheets: https://www.rstudio.com/resources/cheatsheets/
Explore geoms and other plot elements with the data you have used and/or your own data
msleep
?msleep
OrchardSprays
data()
pdf("./Plots/todays_plots.pdf")
print(basic.plot)
print(plot.with.linear.smooth)
print(categorical.plot)
print(CO2.plot)
graphics.off()
all other base save functions available:
bmp()
, jpeg()
, etc
ggsave: saves last plot and guesses format from file name
ggsave("./Plots/todays_plots.jpeg", basic.plot)
base R plot
function has methods for many different object types
plot(iris)
base R plot
function has methods for many different object types
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi,
data = LifeCycleSavings)
plot(lm.SR)
Find an interesting data set on Dryad.org, reproduce a figure from the article using ggplot2
Example: try to reproduce figure 1 and 4 from
Low-Décarie, E., Fussmann, G. F., Bell, G., Low-Decarie, E., Fussmann, G. F., Bell, G., Low-Décarie, E., Fussmann, G. F. & Bell, G. 2014 Aquatic primary production in a high-CO2 world. Trends Ecol. Evol. 29, 1–10.
paper
data
full scripts also available on github (old ugly code!)
ggplot can be extended for plotting specific classes of objects
autoplot
and
fortify
ggfortify
provides autoplot
and
fortify
for common models
require(ggfortify)
autoplot(lm.SR)
help(package=ggfortify)
if(!require(devtools)) {install.packages("devtools")}
require(devtools)
if(!require(ggvegan)) {install_github("gavinsimpson/ggvegan")}
require(ggvegan)
data(dune)
data(dune.env)
sol <- cca(dune ~ A1 + Management,
data = dune.env)
autoplot(sol)
autoplot(sol) + theme_bw()
base R plot
function has methods for many different object types
normal.plot <- plot(sol)
CO2.plot +
scale_y_continuous(name = "CO2 uptake rate",
breaks = seq(5,50, by= 10),
labels = seq(5,50, by= 10),
trans="log10")
CO2.plot+
scale_colour_brewer()
CO2.plot+
scale_colour_manual(values=c("nonchilled"="red",
"chilled"="blue"))
Bonus!!! Wes Anderson colour palette
Bonus!!! Wes Anderson colour palette
if(!require(devtools)) {install.packages("devtools")}
require(devtools)
if(!require(wesanderson)){
devtools::install_github("karthik/wesanderson")}
require(wesanderson)
Bonus!!! Wes Anderson colour palette
require(wesanderson)
basic.plot +
scale_color_manual(values = wesanderson::wes_palette("Darjeeling",3))
if(!require(gridExtra)) {install.packages("gridExtra")}
require(gridExtra)
grid.arrange(basic.plot, CO2.plot)
Sub-plots can be aligned and matched in size
basic.plot.table <- ggplot_gtable(ggplot_build(basic.plot))
CO2.plot.table <- ggplot_gtable(ggplot_build(CO2.plot))
maxWidth = grid::unit.pmax(basic.plot.table$widths[2:3],
CO2.plot.table$widths[2:3])
basic.plot.table$widths[2:3] <- as.list(maxWidth)
CO2.plot.table$widths[2:3] <- as.list(maxWidth)
Sub-plots can be aligned and matched in size
grid.arrange(basic.plot.table,
CO2.plot.table,
ncol=1)
theme_set(theme())
or
plot+theme()
bw <- basic.plot+theme_bw()
grey <- basic.plot+theme_bw()
grid.arrange(basic.plot, bw, grey, nrow=1)
mytheme <- theme_grey() +
theme(plot.title = element_text(colour = "red"))
mytheme_plot <- basic.plot + mytheme
grid.arrange(basic.plot, mytheme_plot, nrow=1)
Using figure from previous challenge (or other dryad.org paper/data), edit figure to match a journal's style requirements
Example: try to reproduce Figure 3 in: Lucek K, Sivasundar A, Roy D, Seehausen O (2013) Repeated and predictable patterns of ecotypic differentiation during a biological invasion: lake-stream divergence in parapatric Swiss stickleback. Journal of Evolutionary Biology 26(12): 2691–2709.