ggplot2A grammar of graphics is
We start with the data and the axes.
ggplot(
data = <dataset>,
aes(<mappings>)
) +
geom_function(
aes(<additional mappings>), # <- This line is optional
stat = <stat>, position = <position>) +
coordinate_function() +
facet_function() +
scale_function() +
theme_function()
You can layer multiple geoms together to get a more complicated graphic
Next, we specify the geometric object we want to use to show the data.
Each geom has required aesthetic mappings as well as optional mappings.
| geom | required aesthetics | optional aesthetics |
|---|---|---|
| point | x, y | color, shape, size, alpha |
| text | x, y, label | color, shape, size, angle, alpha, font family, font face, hjust, vjust |
| line | x, y | color, group, linetype, size |
| rect | xmin, xmax, ymin, ymax | color, fill, alpha, linetype, size |
| abline | intercept, slope | alpha, color, linetype, size |
| segment | x, y, xend, yend | alpha, color, linetype, size |
| bar | x | alpha, color, fill, linetype, size, weight |
ggplot2 reference card to see all the geoms and their required mappingsArrange geoms that would otherwise use the same space
Arrange geoms that would otherwise use the same space
Sometimes, we want to compute statistics based on our data, and map geoms to those computations.
Sometimes, we want to compute statistics based on our data, and map geoms to those computations.
Smooth or Trend Line: stat_smooth()
geom_line(aes(x = after_stat(x),
y = after_stat(y))) +
geom_ribbon(
aes(x = after_stat(x),
ymin = after_stat(ymin),
ymax = after_stat(ymax)))
Boxplots (5-number summary): stat_boxplot()
geom_boxplot()
# A shortcut for something like this:
geom_rect(aes(xmin = x - after_stat(width)/2,
xmax = x + after_stat(width)/2,
ymin = after_stat(lower),
ymax = after_stat(upper)) +
geom_segment(aes(x = x - after_stat(width)/2,
xend = x + after_stat(width)/2,
y = after_stat(middle),
yend = after_stat(middle))) +
geom_segment(aes(x = x, xend = xend,
y = after_stat(lower),
yend = after_stat(ymin))) +
geom_segment(aes(x = x, xend = xend,
y = after_stat(upper),
yend = after_stat(ymax)))
Sometimes, we want to compute statistics based on our data, and map geoms to those computations.
bar plots (count up data by category): stat_count()
geom_bar(aes(x = x, y = after_stat(count)))
# A shortcut for something like this:
geom_rect(aes(xmin = x - after_stat(width),
xmax = x + after_stat(width),
ymin = 0,
ymax = after_stat(count)))
Histograms (cut into bins, then count): stat_bin()
geom_histogram(aes(x = x, y = after_stat(count)))
# A shortcut for something like this:
geom_rect(aes(xmin = after_stat(xstart),
xmax = after_stat(xend),
ymin = 0,
ymax = after_stat(count)))
Frequency Polygons (cut into bins, then count): stat_bin()
geom_freqpoly(aes(x = x, y = after_stat(count)))
# A shortcut for something like this:
geom_line(aes(x = x,
y = after_stat(count)))
Sometimes, we want to compute statistics based on our data, and map geoms to those computations.
density plots: stat_density()
geom_density(aes(x = x, y = after_stat(density)))
# A shortcut for something like this:
geom_line(aes(x = x, y = after_stat(density)))
# Or this (if fill is specified)
geom_ribbon(aes(x = x, ymin = 0, ymax = after_stat(density)))
eCDF (sort, calculate proportion <= each x value): stat_ecdf()
geom_step(aes(x = x, y = after_stat(count)))
# A shortcut for something like this:
# Vertical piece
geom_segment(aes(x = x_prev,
xend = x_prev,
y = after_stat(prop_prev),
yend = after_stat(prop))) +
# Horizontal piece
geom_segment(aes(x = x_prev,
xend = x,
y = after_stat(prop),
yend = after_stat(prop)))
By default, ggplot2 chooses reasonable default scales
scale_{aesthetic}_{type}
manual - available for almost all scalescontinuous, discrete, date, datetime
scale_*_log10, scale_*_reverse, scale_*_sqrtColor/fill have special scales:
scale_*_brewer, scale_*_greytayloRswift has album-themed color palettes. ggsci has palettes from Futurama, Simsons, StarTrek, Tron, Rick and Morty and various news outlets
Color/fill have special scales:
scale_*_brewer, scale_*_greyscale_*_distiller (discrete scale interpolated), scale_*_gradient (low to high), scale_*_gradient2 (bidirectional)
log10 is useful)We’ll talk more about color next time
+ coord_xxx()
When there are too many categorical variables, subplots can help!
facet_wrap() - no ordering
facet_grid(rowvar ~ colvar) - align in a grid
More about this next time!
