cars %>%
ggplot(aes(x = speed, y = dist))RAdelaide 2024
July 9, 2024
ggplot2ggplot2 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
Everything is added in layers
tibble)x & y co-ordinatescolour, fill, shape, size, linetypealpha)carsspeed (mph)distance each car takes to stopx vs y plot using pointsspeeddistancex & ygeom_point() after calling ggplot()
+ after ggplot() says “But wait! There’s more…”geom_point() after calling ggplot()
+ after ggplot() says “But wait! There’s more…”What visualisations could we produce to inspect pigs?
dose as the predictorlen will always be the response variablecolour is generally applied to shape outlinesggplot2 will always separate multiple values/categoryfacet_wrap()
geom_jitter() will add a small amount of noise to separate pointsdose is a clearly a categorical variable with an orderR these are known as factors
levelsggplot() will automatically place character columns in alphanumeric orderfactor with levelsfctlen values \(\implies\) turn into quantiles# A tibble: 60 × 5
len supp dose rank q
<dbl> <chr> <chr> <dbl> <dbl>
1 4.2 VC Low 1 0.0167
2 11.5 VC Low 15 0.25
3 7.3 VC Low 6 0.1
4 5.8 VC Low 3 0.05
5 6.4 VC Low 4 0.0667
6 10 VC Low 11.5 0.192
7 11.2 VC Low 13.5 0.225
8 11.2 VC Low 13.5 0.225
9 5.2 VC Low 2 0.0333
10 7 VC Low 5 0.0833
# ℹ 50 more rows
suppgeom_smooth() will add a line of best fit
stat_smooth()lm, loess or gamaesthetic set in the call to ggplot() is passed to every subsequent layercolour = supp to geom_point() will only colour pointsaes()se bands and switches to lmR?pch pageaestheticsize can also work either wayscale_x_continuous() & scale_y_continuous()scale_colour_brewer() allows pre-defined palettes
RColorBrewerscale_colour_viridis_b/c/d()
_b()), continuous (_c()) or discrete (_d())scale_colour_manual() takes a vector of colours
c()theme()theme() pggplot2 supplies several complete themestheme_grey() by defaulttheme_bw() after p
theme_void(), theme_classic(), theme_minimal()element_*() functions
element_text()element_line()element_rect()element_blank()element_rect()
colour sets the rectangle outline colourfill sets the rectangle filltext = element_text()
labs()
scale_x/y_continuous()p +
ggtitle("Odontoblast Length in Guinea Pigs") +
labs(colour = NULL) +
theme(
rect = element_rect(fill = "#204080"),
text = element_text(colour = "grey80", family = "Palatino", size = 14),
panel.background = element_rect(fill = "steelblue4", colour = "grey80"),
panel.grid = element_line(colour = "grey80", linetype = 2, linewidth = 1/4),
axis.text = element_text(colour = "grey80"),
legend.background = element_rect(fill = "steelblue4", colour = "grey80"),
legend.key = element_rect(colour = NA),
legend.position = "inside",
legend.position.inside = c(1, 0),
legend.justification = c(1, 0),
plot.title = element_text(hjust = 0.5, face = "bold"),
)
Export in the Plots paneA fabulous resource: https://r-graphics.org/
