library(tidyverse)
ASI: Introduction to R
September 2, 2025
BasicVisualisation.R
# A tibble: 333 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen 36.7 19.3 193 3450
5 Adelie Torgersen 39.3 20.6 190 3650
6 Adelie Torgersen 38.9 17.8 181 3625
7 Adelie Torgersen 39.2 19.6 195 4675
8 Adelie Torgersen 41.1 17.6 182 3200
9 Adelie Torgersen 38.6 21.2 191 3800
10 Adelie Torgersen 34.6 21.1 198 4400
# ℹ 323 more rows
# ℹ 2 more variables: sex <chr>, year <dbl>
R
comes with some very powerful plotting capabilities
graphics
ggplot2
changed everythingcars
dataset
speed
(mph)dist
(ft) each car takes to stopmy_penguins
dataset to compare flipper length and body massboxplot()
can also create simple figures easilyR
formula notation
y ~ x
\(\implies\) y
depends on x
, ory ~ x
\(\implies\) y
is a function of x
ggplot2
ggplot2
has become the industry standard for visualisation (Wickham 2016)tidyverse
Taken from https://r.qcbs.ca/workshop03/book-en/grammar-of-graphics-gg-basics.html
ggplot()
was followed by +
R
: “But wait, there’s more to come!”method = "lm", se = FALSE
aes()
will send this mapping to all layers
sex
of penguins on this figurestat_smooth()
ggplot2
scale_*()
functions?pch
ggplot(my_penguins) + # 1: Define the data layer
aes(x = body_mass_g, y = flipper_length_mm, colour = species) + # 2: Mappings
geom_point(aes(shape = sex)) + # 3: Geometry with layer-specific aes
facet_wrap(~sex, scales = "free") + # 4: Facets
stat_smooth(method = "lm", se = FALSE) + # 5: Statistics
scale_shape_manual(values = c(1, 19)) # 6: Co-ordinates (i.e. scales)
scale_colour_brewer()
is an excellent starting pointggplot(my_penguins) + # 1: Define the data layer
aes(x = body_mass_g, y = flipper_length_mm, colour = species) + # 2: Mappings
geom_point(aes(shape = sex)) + # 3: Geometry with layer-specific aes
facet_wrap(~sex, scales = "free") + # 4: Facets
stat_smooth(method = "lm", se = FALSE) + # 5: Statistics
scale_shape_manual(values = c(1, 19)) + # 6: Co-ordinates (i.e. scales)
scale_colour_brewer(palette = "Set1")
brewer
palettes using RColorBrewer::display.brewer.all()
scale_colour_manual(values = c("red", "blue", "green"))
scale_colour_viridis_d()
ggthemes
scale_colour_colorblind()
library(ggthemes)
ggplot(my_penguins) + # 1: Define the data layer
aes(x = body_mass_g, y = flipper_length_mm, colour = species) + # 2: Mappings
geom_point(aes(shape = sex)) + # 3: Geometry with layer-specific aes
facet_wrap(~sex, scales = "free") + # 4: Facets
stat_smooth(method = "lm", se = FALSE) + # 5: Statistics
scale_shape_manual(values = c(1, 19)) + # 6: Co-ordinates (i.e. scales)
scale_colour_colorblind() # Stoopid American spelling
scale_x_continuous()
for continuous datascale_x_discrete()
for categorical datascale_colour_gradient()
for continuous data (e.g. heatmaps)theme_bw()
applies a generally usable set of defaultslibrary(ggthemes)
ggplot(my_penguins) + # 1: Define the data layer
aes(x = body_mass_g, y = flipper_length_mm, colour = species) + # 2: Mappings
geom_point(aes(shape = sex)) + # 3: Geometry with layer-specific aes
facet_wrap(~sex, scales = "free") + # 4: Facets
stat_smooth(method = "lm", se = FALSE) + # 5: Statistics
scale_shape_manual(values = c(1, 19)) + # 6: Co-ordinates (i.e. scales)
scale_colour_colorblind() + # Stoopid American spelling
theme_bw() # 7: The overall theme
theme_set(theme_bw())
at the start of a session
aes()
/ geom_point()
geom_line()
, geom_abline()
, geom_hline()
, geom_vline()
geom_boxplot()
, geom_violin()
geom_histogram()
, geom_density()
geom_bar()
, geom_col()
+ geom_errorbar()
geom_tile()
, geom_raster()
, geom_rect()
binwidth
and setting colour
(outline)aes()
can also be set in the first call to ggplot()
ggplot2
customisationggplot2
options can also be effective:
corrplot()
from the package corrplot
pheatmap()
from the package pheatmap
VennDiagram
UpSetR
(is actually ggplot
)pigs
dataset and create a boxplot
dose
across the x-axisgeom_violin()
as an alternative