2. In today’s session
• Principles behind exploratory analyses
• Plotting data out on to popular exploratory graphs
• Plotting Systems in R
• Base (Week1)
• Lattice (Week2)
• GGPLOT2 (Week2)
• Choosing and using Graphic Devices aka the output formats
Scripts can be downloaded at:
https://www.dropbox.com/s/ii1yj8f650d4l1q/lesson1.r?dl=0
https://www.dropbox.com/s/eme44h6lrhn775l/final.r?dl=0
3. Principles behind exploratory analyses
• Show comparisons
• Show causality, mechanism, explanation
• Show multivariate data
• Integrate multiple modes of evidence
• Describe and document the evidence
• Content is king
• SPEED
14. Base plots: Scatterplot
R code
data1 = read.table("scatter1.txt", h=T)
data2 = read.table("scatter2.txt", h=T)
#Color
with(data1, plot(xv, ys, col="red"))
with(data1, abline(lm(ys~xv)))
#shape
with(data2,
points(xv2, ys2, col="blue",
pch =11))
Symbol shape
15. Base plots: Scatterplot
R code
data1 = read.table("scatter1.txt", h=T)
data2 = read.table("scatter2.txt", h=T)
#Color
with(data1, plot(xv, ys, col="red"))
with(data1, abline(lm(ys~xv)))
#shape
with(data2,
points(xv2, ys2, col="blue",
pch =11))
Symbol shape
16. Base plots: Using par for multiple plots
R code
par(mfrow=c(1,2))
with(data1, plot(xv, ys, col="red"))
with(data1, abline(lm(ys~xv)))
#Plot2
with(data2,
plot(xv2, ys2, col="blue",
pch =11))
title(“My Title", outer=TRUE)
17. Par: To set global settings
R code
mfrow(
mar=c(5.1,4.1,4.1,2.1),
oma=c(2,2,2,2)
)
18. Lattice
productivity = read.table("productivity.txt",h=T)
# of species in forest against differing productivity
library(lattice)
#plotting
xyplot( x~y, productivity,
xlab=list(label="Productivity"),
ylab=list(label="Mammal Species"))
R code
Formular
Data frame
19.
20. Lattice
productivity = read.table("productivity.txt",h=T)
# of species in forest against differing productivity
library(lattice)
#plotting
xyplot( x~y, productivity,
xlab=list(label="Productivity"),
ylab=list(label="Mammal Species"))
xyplot( x~y | f, productivity,
xlab=list(label="Productivity"),
ylab=list(label="Mammal Species"))
R code
Formular
Data frame
given
21.
22. ggplot2
• Grammar of graphics (gg)
• Based on GRID plotting system, cannot be
mixed with base
ggplot2.org
23. ggplot
Components
• Data & relationship
• GEOMetric Object
• Statistical transformation
• Scales
• Coordinate system
• Facetting
28. ggplot
Components
• Data & relationship ✔
• GEOMetric Object
• Statistical transformation
• Scales
• Coordinate system
• Facetting
R code
Rmbr to change
month into a
factor
data.frame
Aesthetics function which maps the relationships
ggplot(weather, aes(x=month, y=upper))+
geom_boxplot()
34. qplot
A separate function which wraps ggplot, for simpler syntax
R code
qplot(month, upper, fill=month, data=weather, facets = ~yr, geom="bar",
stat="identity")
37. Final Challenge
R code
library(ggplot2)
#Reads in data
data = read.csv("final.csv")
#Preparing for the rectangle background
areas=unique(subset(data, select=c(Planning_Area,Planning_Region)))
areas=areas[order(areas$Planning_Region),]
areas$rectid=1:nrow(areas)
rectdata = areas %>% group_by(Planning_Region) %>% summarise(xstart=min(rectid)-
0.5,xend= max(rectid)+0.5)
#Order the levels
data$Planning_Area=factor(data$Planning_Area,
levels=as.character(areas[order(areas$Planning_Region),]$Planning_Area))