Visualising and Plotting Data

ASI: Introduction to R

Author
Affiliation

Dr Stevie Pederson

Black Ochre Data Labs
The Kids Research Institute Australia

Published

September 2, 2025

Visualisation in R

Start a New R Script

  • Call the new script: BasicVisualisation.R
  • Load our favourite package at the top of the script
library(tidyverse)
  • Load the my_penguins dataset
my_penguins <- read_csv("data/my_penguins.csv")

Introducing The Penguins

my_penguins
# 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>
  • Contains multiple measurements for penguins recorded around Palmer Station, Antarctica
  • Slightly modified version from https://allisonhorst.github.io/palmerpenguins/

Base Plotting in R

  • R comes with some very powerful plotting capabilities
    • Provided in the base package graphics
    • Always loaded with every session
  • Examples are often extremely helpful
  • People used happily for decades
    • The release of ggplot2 changed everything
  • Let’s quickly explore base plotting before moving to the good stuff

Base Plotting In R

  • Simple plots are usually easy
    • Complex figures can get really messy
  • Using the cars dataset
    • speed (mph)
    • dist (ft) each car takes to stop
plot(cars)

Base Plotting In R

  • The first two columns were automatically placed on the x & y axis
  • We could set values for x & y manually
  • Automatically decided to plot using points
plot(x = cars$speed, y = cars$dist)

Base Plotting In R

  • Using the my_penguins dataset to compare flipper length and body mass
plot(x = my_penguins$body_mass_g, y = my_penguins$flipper_length_mm) 

Base Plotting In R

  • The function boxplot() can also create simple figures easily
  • For categorical variables (i.e. factors) we use R formula notation
    • y ~ x \(\implies\) y depends on x, or
    • y ~ x \(\implies\) y is a function of x
## Make a simple boxplot showing the weights by species
boxplot(body_mass_g ~ species, data = my_penguins)
  • The dependent variable will always appear on the y-axis
  • The predictor will always appear on the x-axis

Base Plotting In R

  • We can also use combinations of predictor variables
## Separate by species and sex
boxplot(body_mass_g ~ sex + species, data = my_penguins)

Base Plotting In R

  • Histograms can be produced on an individual column
    • The number of breaks can be set manually
  • The default is pretty useful here
    • Generally simple figures without complexity
hist(my_penguins$body_mass_g, breaks = 20, xlab = "Body Mass (g)")

References

Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
Wilkinson, Leland. 2005. The Grammar of Graphics. Springer New York, NY. https://doi.org/https://doi.org/10.1007/0-387-28695-0.