ggplot2 (grammar of graphics plot) is a twist on the traditional way of displaying graphics, and will differ slighly compared with the plot functions in R.

Layers

The general idea of GOG (grammar of graphics) is that graphics can be seperated into layers.

From Wickham’s paper, the layers can be summarised as follows:

  • data or aesthetic mappings, which describe the variables (e.g. x ~ y plot)
  • geometric objects, which describe what you see, (e.g. line plot, boxplot, scatterplot)
  • scaless which map values to things like colour, size, or shape.
  • facet breaks data into subsets to display as small multiples

Basic Plotting

The most basic way to use ggplot2 is using qplot this is extremely similar to your normal plot function in R.

with(mtcars, plot(cyl, mpg, pch = carb, col = gear)) 

init-repo

qplot(cyl, mpg, data=mtcars, shape=as.factor(carb), col=gear)

init-repo

Though this can be easily broken down into several layers.

ggplot(mtcars, aes(cyl, mpg))+aes(shape=as.factor(carb), color=gear)+geom_point()

init-repo

Breaking things into layers is useful, because you can save the layers and generate different plots quite easily, if needed.

p <- ggplot(mtcars, aes(cyl, mpg))
p + geom_point()+facet_grid(carb~.)+aes(color=gear)

init-repo

p + geom_point()+facet_grid(.~gear)+aes(shape=as.factor(carb))

init-repo

Adding Lines to Scatter Plots

Adding additional geometric objects is the same as adding an extra layer.

In base R

with(mtcars, plot(cyl,mpg))
with(mtcars, lines(lowess(cyl,mpg),col='red'))

init-repo

Under ggplot2

p <- ggplot(mtcars, aes(cyl,mpg)) + geom_point() 
p + geom_smooth(method='loess')

init-repo

You can see we merely add an additional geometric layer which has our smoother.

We can even define our own smoothers if we wish:

library(splines)
p + geom_smooth(method='lm', formula=y~ns(x,2))

init-repo

That should be more than enough to get started!