Week 2: Principles of data visualisation- Week 3: Grammar of Graphics; aesthetics and attributes
- Week 4: Major visualisation tools
- Week 5: Customising visualisations (scales, themes, and labels)
By the end of the session, you should be able to:
ggplot2 plot as a set of layersDownload the Week 3 files from NOW and move them into your RStudio Project folder.
There are two exercise files for today:
01_building_ggplot_layers.Rmd02_aesthetics_vs_attributes.RmdBefore we name the grammar, look at the plot and identify what it is made from.
What do you notice about the data, visual marks, colours, lines, labels, and layout?
| Component | Example | Description |
|---|---|---|
| Data | d_spellname |
The dataset being plotted |
| Aesthetics (aes) | x = dur, y = rt |
How data variables map to visual properties |
| Geometries (geom) | geom_point() |
The type of plot element (points, bars, lines) |
| Statistics (stat) | stat_smooth() |
Transformations of data (e.g., regression lines) |
| Scales | scale_x_log10() |
How data values are converted to aesthetics |
| Facets | facet_wrap(~group) |
Splitting data into small multiples |
| Coordinate system | coord_polar() |
The space where data are drawn (Cartesian, polar) |
| Theme | theme_minimal() |
Non-data display elements (fonts, text, grids, etc.) |
ggplot2 Uses A Grammar ggplot2 is an R package for creating data visualisations.A statistical graphic connects three main things:
Other layers can then summarise, split, transform, label, or style the plot.
Think about language: grammar tells us how words can be combined into sentences.
For plots, the grammar tells us how data, mappings, and visual marks can be combined.
The practical skill for today is to read a plot as layers: What data are used? What is mapped? What is drawn? What else has been added?
ggplot(data = ...)mapping = aes()geom_...()ggplot(d_spellname, aes(x = dur, y = rt))
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_quantile()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_rug()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + geom_quantile() + geom_rug()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + facet_grid( ~ modality)
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + stat_smooth(method = "lm", se = FALSE)
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + coord_trans(x = "log", y = "log")
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + coord_flip()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + theme_dark()
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + theme(panel.background = element_blank())
Complete RMarkdown document 01_building_ggplot_layers.Rmd.
This exercise practises reading a plot as a sequence of ggplot2 layers.
ggplot(data = d_spellname,
mapping = aes(x = dur,
y = rt,
colour = group)) +
geom_point(size = .75) +
stat_smooth(method = "lm", se = F) +
labs(x = "Response duration (in ms)",
y = "Reaction time (in ms)") +
theme(legend.position = "right",
legend.justification = "top",
legend.direction = "vertical")
Aesthetics: visual properties mapped to variables (data-driven) e.g. aes(x = duration, y = time, colour = group) → colour changes with data and usually creates a scale / legend
Attributes: visual properties set manually (constant) e.g. geom_point(colour = "blue", size = 2) → colour is fixed and does not describe a variable
Think:
Aesthetic = mapping (data → visual)
Attribute = setting (visual → fixed property)
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point(colour = "red")
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point(aes(colour = modality))
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point(aes(colour = modality)) + stat_smooth(method = "lm")
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point() + stat_smooth(aes(colour = modality), method = "lm")
ggplot(d_spellname, aes(x = dur, y = rt)) + geom_point(aes(colour = modality)) + stat_smooth(aes(colour = modality), method = "lm")
ggplot(d_spellname, aes(x = dur, y = rt, colour = modality)) + geom_point() + stat_smooth(method = "lm")
ggplot(d_spellname, aes(x = dur, y = rt, shape = modality)) + geom_point(size = 2.5, colour = "red")
ggplot(d_spellname, aes(x = dur, y = rt, colour = modality)) + geom_point(size = 2.5)
ggplot(d_spellname, aes(x = dur, y = rt,
colour = modality,
shape = modality)) +
geom_point(size = 2.5)
Sometimes redundant coding is helpful:
Avoid this when it creates too many visual groups.
geom_point()x, y, shape, colour, fill, size, alpha, stroke, group
geom_bar()x, y, colour, fill, linewidth, linetype, alpha, width, group
geom_boxplot()x, y, lower, xlower, upper, xupper, middle, xmiddle, ymin, xmin, ymax, xmax, weight, colour, fill, size, alpha, shape, linetype, linewidth, width, group
Complete RMarkdown document 02_aesthetics_vs_attributes.Rmd.
This exercise practises deciding whether a visual property should be mapped to a variable or set to a fixed value.
Before mapping a variable to colour, shape, size, or another aesthetic, ask what kind of variable it is.
| Variable type | Examples | Usually works well with |
|---|---|---|
| Continuous | age, reaction time, vocabulary score | position, gradients, sometimes size |
| Categorical | modality, group, condition | position, colour hue, shape, linetype, facets |
For today, focus on where aesthetics go in the code. We will use variable type more directly next week when choosing plot types.
If time permits, revisit your plots and ask whether each aesthetic improves the comparison.
Use the two exercise files from today as your main practice for Week 3.
For support with this week’s material, use:
ggplot2. Online chapterUse the ggplot2 online book as an optional reference when you want more detail on aesthetics, scales, and facets. Online book
Identify a dataset you would like to use for the formative assessment: post a description on our Teams channel.
Think about what you would like to visualise about these data:
Andrews, M. (2021). Doing data science in R: An introduction for Social Scientists. SAGE Publications Ltd.
Wickham, H. (2010). A layered grammar of graphics. Journal of Computational and Graphical Statistics, 19(1), 3–28.
Wickham, H. (2016). ggplot2: Elegant graphics for data analysis. Springer.
Wickham, H., & Grolemund, G. (2016). R for data science: Import, tidy, transform, visualize, and model data. O’Reilly Media, Inc.
Wilkinson, L. (2005). The grammar of graphics (2nd ed.). Springer.