author: Etienne Low-Décarie transition: rotate
January 5, 2017
Presentation: https://low-decarie.github.io/ggplot_workshop/#/
Code: https://github.com/low-decarie/ggplot_workshop
- Your first ggplot plot
- basic scatter plot
- Exercise 1
- Grammar of graphics
- More advanced plots
- Available plot elements and when to use them
- Exercise 2
Tomorrow: 3. Saving a plot
- Exercise 3
- Challenge
- Expanding ggplot
- Fine tuning your plot
- colours
- themes
- Maps
- Always work from a script
- Use carriage returns and indentation
object <- function(argument1="value1",
argument2="value2",
argument3="value3")
- Create your own new script
- refer to provided code only if needed
- don't just copy paste from the presentation
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)
- data
- aesthetics (aes)
- transformation
- geoms (geometric objects)
- axis (coordinate system)
- scales
- x,y : position along the x and y axis
- colour: the colour of the point
- group: what group a point belongs to
- shape: the figure used to plot a point
- linetype: the type of line used (solid, dashed, etc)
- size: the size of the point or line
- alpha: the transparency of the point
- point: scatterplot
- line: line plot, where lines connect points by increasing x value
- path: line plot, where lines connect points in sequence of appearance
- boxplot: box-and-whisker plots, for categorical y data
- bar: barplots
- histogram: histograms (for 1-dimensional data)
Editing an element produces a new graph e.g. just change the coordinate system
-
- create a simple plot object
plot.object<-qplot()
or
plot.object<-ggplot()
-
- add graphical layers/complexity
plot.object<-plot.object+layer()
-
- repeat step 2 until satisfied
-
- print your object to screen (or to graphical device)
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()
Your browser does not support iframes.
</iframe>cheatsheets: https://www.rstudio.com/resources/cheatsheets/
<iframe src="https://www.rstudio.com/resources/cheatsheets/" width="1000" height="800">Your browser does not support iframes.
</iframe>Explore geoms and other plot elements with the data you have used and/or your own data
msleep
?msleep
OrchardSprays
data()
- Saving a plot
- Exercise 3
- Challenge
- Expanding ggplot
- Fine tuning your plot
- colours
- themes
- Maps (time permitting)
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)
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)
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)
class: small-code
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)
class: small-code
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()
dark <- basic.plot+theme_dark()
minimal <- basic.plot+theme_minimal()
grid.arrange(basic.plot, dark, minimal, nrow=1)
class: small-code
mytheme <- theme_grey() +
theme(plot.title = element_text(colour = "red"))
mytheme_plot <- basic.plot + mytheme
grid.arrange(basic.plot, mytheme_plot, nrow=1)
Your browser does not support iframes.
</iframe>#install.packages("xkcd")
#install xkcd font: "http://simonsoftware.se/other/xkcd.ttf"
#import font : font_import(pattern = "[X/x]kcd", prompt=FALSE)
#loadfonts()
require(xkcd)
require(extrafont)
xrange <- range(iris$Sepal.Length)
yrange <- range(iris$Sepal.Width)
print(basic.plot+
xkcdaxis(xrange,yrange)+
theme(text=element_text(family = "xkcd")))
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.