Creating Graphics with ggplot2

Above, we used the qplot function to build ggplot2 objects in one function call. Sometimes, you may need more flexibility than qplot provides. Alternately, you may want to write a more verbose description of your plot to make your code easier to read. To do this, you create your plot in several parts:

  1. You call the ggplot function to create a new ggplot object, define the input data, and define aesthetic mappings

  2. You add layers to the ggplot object

Note that you add layers (and options) to a ggplot object by using the + operator.

As an example, we could create a plot identical to the one we started with using these statements:

> plt <- ggplot(data=d, mapping=aes(x=a, y=b)) + geom_point()
> summary(plt)
data: a, b, c [10x3]
mapping:  x = a, y = b
faceting: facet_null()
-----------------------------------
geom_point: na.rm = FALSE
stat_identity:
position_identity: (width = NULL, height = NULL)

To create ggplot objects without qplot, you begin by using the ggplot function.

ggplot(data, mapping = aes(), ..., environment = globalenv())

Here is a description of the arguments to ggplot2:

ArgumentDescriptionDefault
dataThe default data frame for the plot 
mappingDefault list of aesthetic mappings for the plotaes()
environmentEnvironment in which the aesthetics should occurglobalenv()
...  

The ggplot function returns a new ggplot object with no layers. You can’t actually print a chart from this object because no layers are defined:

> ggplot(data=d, mapping=aes(x=a, y=b))
Error: No layers in plot

Typically, you specify aesthetic mappings with the aes function:

aes(x, y, ...)

The x argument specifies the x value, the y argument specifies the y value, and other arguments specify aesthetics to map as name/value pairs. See the documentation for ggplot2 for alternate ways to map aesthetics including aes_string and aes_auto. As an example, to finish specifying a plot, you need to add layers. You can create a new layer with the layer function:

layer(...)

You specify the geometric objects using short names like "point". Using our earlier example, we could define our plot object with:

> plt <- ggplot(data=d, mapping=aes(x=a, y=b)) + layer("point")

The layer function allows you to specify geometric objects as name value pairs. You do not need to specify the full function name, but simply need to part after geom_.

For reference, here is a description of the available geometric functions:

Geometric FunctionDescription
geom_ablineA line, specified by a slope and intercept
geom_areaArea plot (a continuous analog to a bar plot)
geom_barBar plot
geom_bin2dHeatmap of two-dimensional bins
geom_blankBlank geometric object; doesn’t draw anything
geom_boxplotBox plot
geom_contourContour plot
geom_crossbarCrossbar plot (like a box plot, but without the whiskers and extreme values)
geom_densityDensity plot
geom_density2dTwo-dimensional density plot
geom_errorbarError bars (typically added to other plots like bar plots, point plots, and line plots)
geom_errorbarhHorizontal error bars
geom_freqpolyFrequency polygon (similar to a histogram)
geom_hexHexagonal objects (typically used with hexagonal binning)
geom_histogramHistogram
geom_hlineA horizontal line
geom_jitterPoints, automatically jittered
geom_lineA line
geom_linerangeAn interval represented by a vertical line
geom_pathA geometric path, connecting a set of points in order
geom_pointPoints
geom_pointrangeA vertical line with a point in the middle (related to crossbars, boxplots, and line-ranges)
geom_polygonA polygon
geom_quantileA set of quantile lines from a quantile regression
geom_rectTwo-dimensional rectangles
geom_ribbonA ribbon (a y range with continuous x values, like Tufte’s famous Napoleon’s march plot)
geom_rugA rug
geom_segmentLine segments
geom_smoothA smoothed condition mean
geom_stepA stepped plot connecting points
geom_textText
geom_tileTiles
geom_vlineVertical line

ggplot2 includes some convenience functions for applying a statistical transformation and adding a layer to a plot. Some of these functions are listed below.

Statistic FunctionDescription
stat_ablineAdds a line with a slope and intercept.
stat_binSplits data into bins then plots as a histogram.
stat_bin2dShows density across two dimensions using rectangles.
stat_binhexShows density across two dimensions using hexagons.
stat_boxplotCreates a box-and-whiskers plot.
stat_contourShows contours of three-dimensional data.
stat_densityPlots density.
stat_density2dPlots density in two dimensions.
stat_functionSuperimposes a function.
stat_hlineAdds a horizontal line.
stat_identityPlots data without a statistical transformation.
stat_qqCalculations for a quantile-quantile plot.
stat_quantileContinuous quantiles.
stat_smoothAdds a smoother.
stat_spokePlots directional data at points (specifying location with x and y, and angle separately).
stat_sumPlots sums of unique values (typically on a scatter plot).
stat_summaryPlots summarized data.
stat_uniquePlots only unique values (removes duplicates).
stat_vlinePlots a vertical line.

You can manually specify different scales with ggplot2; mapping data to different scales lets you control how ggplot2 shows different densities, quantities, or other values. Scales can specify ranges of colors, objects, or labels. The following table shows some of these scale functions :

Scale functionDescription
scale_alphaAlpha channel values (grayscale).
scale_brewerColors derived from scales shown on colorbrewer.org.
scale_continuousContinuous scales.
scale_dateDates.
scale_datetimeDates and times.
scale_discreteDiscrete values.
scale_gradientSmooth gradients between two colors.
scale_gradient2Smooth gradients among three colors.
scale_gradientnSmooth gradients among n colors.
scale_greyGrayscale colors.
scale_hueEvenly spaced hues.
scale_identityUses values without scaling.
scale_linetypeShows differences as line patterns.
scale_manualManually created discrete scales.
scale_shapeDifferent shapes (“glyphs”) for different values.
scale_sizeShows different values as different size objects.

With ggplot2, you can plot data using several different coordinate systems:

Coordinate functionDescription
coord_cartesianCartesian coordinates
coord_equalEqual scale coordinates
coord_flipFlipped Cartesian coordinates
coord_mapMap projections
coord_polarPolar projections
coord_transTransformed Cartesian coordinates

There are two options for faceting data bundled with the ggplot2 package:

Faceting functionDescription
facet_gridLay out panels in a grid
facet_wrapWraps a one-dimensional list of facets into two dimensions

When you are plotting multiple geometric objects (such as multiple bars), you can specify where different objects should be plotted.

Position functionDescription
position_dodgePositions objects by dodging overlaps to the side (lays them out in a non-overlapping way)
position_fillStacks overlapping objects on top of one another
postition_identityDoesn’t adjust the position
position_jitterJitters objects
postion_stackStacks objects