More ggplot2

## for plotting
library(ggplot2)
## for data
library(nlme)
data(BodyWeight)
## check data
head(BodyWeight)
## Grouped Data: weight ~ Time | Rat
##   weight Time Rat Diet
## 1    240    1   1    1
## 2    250    8   1    1
## 3    255   15   1    1
## 4    260   22   1    1
## 5    262   29   1    1
## 6    258   36   1    1
## Create the basis for plotting
base1 <- ggplot(data = BodyWeight, mapping = aes(x = Time, y = weight))
## scatter plot only (save as scatPlot)
scatPlot <- base1 + layer(geom = "point")
scatPlot

plot of chunk unnamed-chunk-1

## Add line (another layer of a line plot) ohh, we forgot to group by
## individuals
scatPlot + layer(geom = "line")

plot of chunk unnamed-chunk-1

## Add lines connecting individuals
scatPlot + layer(geom = "line", mapping = aes(group = Rat))

plot of chunk unnamed-chunk-1

## Add color by Diet to lines
scatPlot + layer(geom = "line", mapping = aes(group = Rat, color = Diet))

plot of chunk unnamed-chunk-1

## Adding summary using stata_sumamry
## http://docs.ggplot2.org/0.9.3.1/stat_summary.html stat_summary(fun.data =
## 'mean_cl_boot', colour = 'red')
scatPlot + layer(geom = "line", mapping = aes(group = Rat, color = Diet)) + 
    layer(geom = "pointrange", stat = "summary", fun.data = "mean_cl_boot")

plot of chunk unnamed-chunk-1