ggplot2HES 505 Fall 2022: Session 23
Matt Williamson
By the end of today you should be able to: * Understand the relationship between the Grammar of Graphics and ggplot syntax
Describe the various options for customizing ggplots and their syntactic conventions
Generate complicated plot layouts without additional pre-processing
Grammar: A set of structural rules that help establish the components of a language
System and structure of language consist of syntax and semantics
Grammar of Graphics: a framework that allows us to concisely describe the components of any graphic
Follows a layered approach by using defined components to build a visualization
ggplot2 is a formal implementation in R
{ggplot2} is a system for declaratively creating graphics,
based on “The Grammar of Graphics” (Wilkinson, 2005).
You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.
| Component | Function | Explanation |
|---|---|---|
| Data |
ggplot(data)
|
The raw data that you want to visualise. |
| Aesthetics |
aes()
|
Aesthetic mappings between variables and visual properties. |
| Geometries |
geom_*()
|
The geometric shapes representing the data. |
| Component | Function | Explanation |
|---|---|---|
| Data |
ggplot(data)
|
The raw data that you want to visualise. |
| Aesthetics |
aes()
|
Aesthetic mappings between variables and visual properties. |
| Geometries |
geom_*()
|
The geometric shapes representing the data. |
| Statistics |
stat_*()
|
The statistical transformations applied to the data. |
| Scales |
scale_*()
|
Maps between the data and the aesthetic dimensions. |
| Coordinate System |
coord_*()
|
Maps data into the plane of the data rectangle. |
| Facets |
facet_*()
|
The arrangement of the data into a grid of plots. |
| Visual Themes |
theme() and theme_*()
|
The overall visual defaults of a plot. |
Bike sharing counts in London, UK, powered by TfL Open Data
| Variable | Description | Class |
|---|---|---|
| date | Date encoded as `YYYY-MM-DD` | date |
| day_night | `day` (6:00am–5:59pm) or `night` (6:00pm–5:59am) | character |
| year | `2015` or `2016` | factor |
| month | `1` (January) to `12` (December) | factor |
| season | `winter`, `spring`, `summer`, or `autumn` | factor |
| count | Sum of reported bikes rented | integer |
| is_workday | `TRUE` being Monday to Friday and no bank holiday | logical |
| is_weekend | `TRUE` being Saturday or Sunday | logical |
| is_holiday | `TRUE` being a bank holiday in the UK | logical |
| temp | Average air temperature (°C) | double |
| temp_feel | Average feels like temperature (°C) | double |
| humidity | Average air humidity (%) | double |
| wind_speed | Average wind speed (km/h) | double |
| weather_type | Most common weather type | character |
ggplot2::ggplot()= link variables to graphical properties
x, y)color, fill)shape, linetype)size)alpha)group)aes() outside as component
= interpret aesthetics as graphical representations
= split variables to multiple panels
Facets are also known as:
= translate between variable ranges and property ranges
The scale_*() components control the properties of all the
aesthetic dimensions mapped to the data.
Consequently, there are scale_*() functions for all aesthetics such as:
positions via scale_x_*() and scale_y_*()
colors via scale_color_*() and scale_fill_*()
sizes via scale_size_*() and scale_radius_*()
shapes via scale_shape_*() and scale_linetype_*()
transparency via scale_alpha_*()
The scale_*() components control the properties of all the
aesthetic dimensions mapped to the data.
The extensions (*) can be filled by e.g.:
continuous(), discrete(), reverse(), log10(), sqrt(), date() for positions
continuous(), discrete(), manual(), gradient(), gradient2(), brewer() for colors
continuous(), discrete(), manual(), ordinal(), area(), date() for sizes
continuous(), discrete(), manual(), ordinal() for shapes
continuous(), discrete(), manual(), ordinal(), date() for transparency
Continuous:
quantitative or numerical data
Discrete:
qualitative or categorical data
Continuous:
quantitative or numerical data
Discrete:
qualitative or categorical data
= interpret the position aesthetics
coord_cartesian()coord_fixed()coord_flip()coord_polar()coord_map() and coord_sf()coord_trans()