% Ben Bolker % Thu Nov 1 12:45:50 2012

ggplot/data visualization lab

cc Licensed under the Creative Commons attribution-noncommercial license. Please share & remix noncommercially, mentioning its origin.

Oxboys example (20-30 minutes)

First load the mlmRev and ggplot2 packages:

library(mlmRev)  ## for Oxboys data
library(ggplot2)

As shown in the lecture, make a minimal plot of the Oxboys data set (see ?Oxboys for information on the data set):

ggplot(Oxboys, aes(x = age, y = height)) + geom_point()

plot of chunk ggplot1

Now play around with it.

Take a look at the online ggplot documentation to get an idea of some of the other options.

Banta et al. fruiting data (20-30 mins)

Get the data set on effects of fruiting and simulated herbivory on Arabidopsis (password-protected), which is described in more detail in the material here (search for “Bolker et al 2009”) and use read.csv to import it (you can call it whatever you want, in the examples below I'll call it dat)

The data are the total number of fruits set (total.fruits), subdivided by status (a nuisance variable, the way the plants were handled); amd (simulated herbivory treatment); nutrient (nutrient level; you may want to create an fNutrient variable within the data set that is a factor instead of a number); rack (which of two experimental racks were used); gen (genotype, again probably should be a factor); popu (population); reg (region).

Start out by mapping the most important variables to the x location, y location, and colour aesthetics:

g0 <- ggplot(dat, aes(x = factor(nutrient), y = total.fruits, colour = amd))

Now experiment with different ways of displaying the data:

See if you can successfully produce Figures 1, 3, and 4 from this PDF (although for Figure 1 you might want to use coord_flip to produce horizontal boxplots with easier-to-read, horizontal labels)

Your own data (20-30 mins, optional)

Alternately, or in addition to the previous example, use ggplot to explore your own data or another data set you can find lying around (try data() in R, or ask the instructor …)