ggplot2: brief introduction

Nancy Reid
Feb 27

Reference: ggplot2, Elegant Graphics for Data Analysis

http://ggplot2.org

  • Chapter 2 surveys qplot; a quick way to convert from plot
  • Chapter 3 illustrates the grammar
  • Chapters 4 and 5 give details on layers, and on constructing common plots
  • Chapters 6 and 7 discuss fine control of scales, axes, legends, positioning
  • Chapter 8 polishing your plots for publication

Chapter 3 overview

Code available as mastery.r at http://ggplot2.org/book

library(ggplot2)
data(mpg)
head(mpg) # we'll use the fuel economony data set
  manufacturer model displ year cyl      trans drv cty hwy fl   class
1         audi    a4   1.8 1999   4   auto(l5)   f  18  29  p compact
2         audi    a4   1.8 1999   4 manual(m5)   f  21  29  p compact
3         audi    a4   2.0 2008   4 manual(m6)   f  20  31  p compact
4         audi    a4   2.0 2008   4   auto(av)   f  21  30  p compact
5         audi    a4   2.8 1999   6   auto(l5)   f  16  26  p compact
6         audi    a4   2.8 1999   6 manual(m5)   f  18  26  p compact

Help needed!

Let's get plotting

plot of chunk unnamed-chunk-2

qplot(displ, hwy, data = mpg, colour = factor(cyl))

More help needed

da

We can change the geometry

plot of chunk unnamed-chunk-3

qplot(displ, hwy, data=mpg, colour=factor(cyl), geom="line") + theme(legend.position = "none")

We can change the geometry

plot of chunk unnamed-chunk-4

qplot(displ, hwy, data=mpg, colour=factor(cyl), geom=" bar", stat="identity", position = "identity") + theme(legend.position = "none")

And make 'combo' plots

plot of chunk unnamed-chunk-5

qplot(displ, hwy, data=mpg, colour=factor(cyl)) + geom_smooth(data= subset(mpg, cyl != 5), method="lm")

And add more complex objects

plot of chunk unnamed-chunk-6

qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()

Legends can have different scales

x <- 1:10
y <- factor(letters[1:5])
qplot(x, x, size = x) 
qplot(x, x, 1:10, colour = x) 
qplot(y, y, 1:10, shape = y) 
qplot(y, y, 1:10, colour = y) 
last_plot() + theme(legend.position = "right")

Faceting

First make a smaller data set:

mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4","f"))

Eliminates 5-cylinder cars and rear-wheel drive cars (29 in all)

No faceting

qplot(cty, hwy, data = mpg2)
+ facet_grid(. ~ .)

Facet by cylinder

qplot(cty, hwy, data = mpg2) + facet_grid(. ~ cyl)

plot of chunk unnamed-chunk-10

Or by two variables

qplot(cty, hwy, data = mpg2) + facet_grid(drv ~ cyl)

plot of chunk unnamed-chunk-11

myplot <- qplot(Ash.dosage, Biomass..g., data = treesAS2, facets = Seedling.species ~ ., color = Seedling.species)

myplot + geom_smooth(method = "lm", formula = y ~ x + I(x^2), se = T)