Chapter 5 Grammar of Graphics (GG) basics
5.1 What is the Grammar of Graphics?
Let’s start with the basics! The package ggplot2
is based on the Grammar of Graphics (GG), which is a framework for data visualization that dissects each component of a graph into individual components, creating distinct layers. Using the GG system, we can build graphs step-by-step for flexible, customizable results.
GG layers have specific names that you will see throughout the workshop:
To make a ggplot
, the data and mapping layers are basic requirements, while the other layers are for additional customization. The layers that are “not required” are still important to think about, but you will be able to generate a basic plot without them.
5.2 Grammar of Graphics layers
5.2.1 A breakdown of common layers
Here is a breakdown of each Grammar of Graphics layer and common arguments for each that can be used as a reference:
- Data:
- your data, in tidy format, will provide ingredients for your plot
- use
dplyr
techniques to prepare data for optimal plotting format - usually, this means you should have one row for every observation that you want to plot
- Aesthetics (aes), to make data visible
x
,y
: variable along the x and y axiscolour
: color of geoms according to datafill
: the inside color of the geomgroup
: what group a geom belongs toshape
: the figure used to plot a pointlinetype
: the type of line used (solid, dashed, etc)size
: size scaling for an extra dimensionalpha
: the transparency of the geom
- Geometric objects (geoms - determines the type of plot)
geom_point()
: scatterplotgeom_line()
: lines connecting points by increasing value of xgeom_path()
: lines connecting points in sequence of appearancegeom_boxplot()
: box and whiskers plot for categorical variablesgeom_bar()
: bar charts for categorical x axisgeom_histogram()
: histogram for continuous x axisgeom_violin()
: distribution kernel of data dispersiongeom_smooth()
: function line based on data
- Facets
facet_wrap()
orfacet_grid()
for small multiples
- Statistics
- similar to geoms, but computed
- show means, counts, and other statistical summaries of data
- Coordinates - fitting data onto a page
coord_cartesian
to set limitscoord_polar
for circular plotscoord_map
for different map projections
- Themes
- overall visual defaults
- fonts, colors, shapes, outlines
5.2.2 Putting these layers together
Let’s try it out! Here are the basic steps to build a plot. You can refer back to these steps throughout the workshop if you need help!
- Create a simple plot object:
plot.object <- ggplot()
- Add geometric layers:
plot.object <- plot.object + geom_*()
- Add appearance layers:
plot.object <- plot.object + coord_*() + theme()
- Repeat step 2-3 until satisfied, then print:
plot.object
orprint(plot.object)
We will come back to this syntax in Chapter 6.4, where we work through the layers in greater depth!