ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book ["gg" stands for Grammar of Graphics].
Documentation can be found on the ggplot website .
Besides implementing Wilkinson's Grammar of Graphics as a set of graphical objets and functions, the package provides also high level functions that can replace the lattice library (as the author says "taking all the good parts of lattice and leaving off the other parts"....).
There are two ways of using ggoplot:
qplot is the basic plotting function, designed to be familiar if you're used to plot (R base), a wrapper for creating a number of different types of plots using a consistent calling scheme.
|qplot(infmor,gnpcap, data=world)||Simple scatterplot|
|qplot(infmor,gnpcap,data=world,colour=continent)||Colours for continents|
|qplot(infmor,gnpcap, data=world,size=popdoc)||Dot size proportional to variable popdoc|
|qplot(infmor,gnpcap, data=world,log="xy")||Simple scatterplot, x and y are log transformed|
|qplot(infmor,gnpcap, data=world,facets=~continent)||Scatterplot for each continent|
|qplot(infmor, data=world, binwidth=40)||Histogram (only x variable defined)|
|qplot(continent, data=world)||Barchart of the factor variable continent|
|qplot(infmor,continent, data=world)||Parallel boxplots (y variable is a factor variable)|
|qplot(continent,infmor)||Dotplots by continet|
|qplot(continent,infmor,geom=c("boxplot","point"))||Boxplots with embedded dotplots by continent|
|qplot(y=infmor,data=world)||Only x specified, produces a sequence plot|
|qplot(infmor,geom="histogram")||A histogram of infant mortality|
|qplot(infmor,gnpcap, data=world,main="A simple scatterplot",xlab="Infant mortality",ylab="GNP per capita")||Scatterplot with a title, and user specified labels for both dimensions|
Any chart has the following components:
Each of these components is present in a chart and can be fully specified and modified, however no need to specify everything for every chart, as ggplot has well chosen defaults for most of them. There are also additional Stats components (useful statistical transformations) and Theme components (background color, grid lines, etc).
library(ggplot2) ggplot(world, aes(x=gnpcap,y=infmor)) + geom_point()
Produces a simple scatterplot. Note the structure of the command line, i.e. ggplot() + geom_point(), i.e. components are added to the base chart information produced by a call to ggplot().
We might also specify the chart and store it as an object:
Then use it and add components