ggplot2 Quick Reference: Anatomy of a plot

Themes

To create the four figures on the first page, we used the built-in theme_grey() and our own theme_invisible().

Input Data

We used the attached file as our data frame.

Complete Plot

s=read.table("sort-obs2.txt", header=T)
ggplot(data=s) + 
scale_x_continuous(formatter="comma", limits=c(0, 100000), name="List Length") +
scale_y_continuous(formatter="comma", limits=c(-1, 25), name="Time [s]") + 
scale_shape_discrete(solid=T) + 
facet_grid(Input~Alg) +
geom_point(mapping=aes(x=Size, y=Time/1000000000, shape=Alg)) + 
geom_smooth(mapping=aes(x=Size, y=Time/1000000000)) +
theme_grey() +
opts(title="Complete Plot", plot.title=theme_text(size=40, vjust=1.5))

Plot Background

s=read.table("sort-obs2.txt", header=T)
ggplot(data=s) + 
scale_x_continuous(formatter="comma", limits=c(0, 100000), name="List Length") +
scale_y_continuous(formatter="comma", limits=c(-1, 25), name="Time [s]") + 
scale_shape_discrete(solid=T) + 
facet_grid(Input~Alg) +
geom_blank(mapping=aes(x=Size, y=Time/1000000000, shape=Alg)) +
theme_grey() +
opts(title="Plot Background", plot.title=theme_text(size=40, vjust=1.5))

Layer 1

s=read.table("sort-obs2.txt", header=T)
ggplot(data=s) + 
scale_x_continuous(formatter="comma", limits=c(0, 100000), name="List Length") +
scale_y_continuous(formatter="comma", limits=c(-1, 25), name="Time [s]") + 
scale_shape_discrete(solid=T) + 
facet_grid(Input~Alg) +
geom_point(mapping=aes(x=Size, y=Time/1000000000, shape=Alg)) + 
theme_invisible() +
opts(title="A Layer", plot.title=theme_text(size=40, vjust=1.5))

Layer 2

s=read.table("sort-obs2.txt", header=T)
ggplot(data=s) + 
scale_x_continuous(formatter="comma", limits=c(0, 100000), name="List Length") +
scale_y_continuous(formatter="comma", limits=c(-1, 25), name="Time [s]") + 
scale_shape_discrete(solid=T) + 
facet_grid(Input~Alg) +
geom_smooth(mapping=aes(x=Size, y=Time/1000000000, shape=Alg)) +
theme_invisible() +
opts(title="Another Layer", plot.title=theme_text(size=40, vjust=1.5))

Other Stuff

d=data.frame(beauty=c(1,2,6,4,4,6,7,8,5,3,6,7,5,4,2,3,5,2,3,3,4,5,6,6,5,4,9,8,6,2,1,1,4,2), intelligence=c(8,4,7,5,4,9,2,3,8,8,3,3,5,5,6,6,6,9,9,7,2,2,4,2,9,8,2,2,3,5,5,6,7,7), speed=c(7,6,9,5,7,6,7,8,5,6,7,5,9,6,9,6,7,8,5,6,5,7,8,5,8,5,7,6,8,9,8,7,7,7), gender=c('m','m','f','m','f','f','f','m','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','f','m','m','m','m','m','m','m','m','m','m'))
> ggplot() + 
+ scale_size_continuous(to=c(4,12)) +
+ geom_point(data=d, mapping=aes(x=intelligence, y=beauty, shape=gender, color=gender, size=speed)) + geom_smooth(data=d, mapping=aes(x=intelligence, y=beauty, color=gender), method="lm", formula=y~log(x), level=0.5, size=1)