+ - 0:00:00
Notes for current slide
Notes for next slide

Essentials 06: Data Visualisation with ggplot2

1 / 14

Session Objectives

Data Visualisation with ggplot2

  • Introduction to ggplot2
  • Boxplot
  • Grouped Boxplot
  • Scatterplot
  • Means plot
2 / 14

Setup

Tasks:

  1. Open the slides for today

  2. Open/create your seminRs project

  3. Download the Rmd document to the r_docs folder of your seminRs project & open it









You can download the Solutions Rmd to check your answers later

3 / 14

Intro to ggplot2

Figures/graphs/plots are a useful way to look at your data

  • Quickly see general trends of data with minimal effort

  • Easy check of data

  • Gives readers a general overview of your results/data


ggplot2 is a really useful package for data visualisation

  • Part of tidyverse

  • Follows The Grammar of Graphics

  • Plots are built in layers from 3 key components: data, coordinate system, & geometric elements (geoms)

  • Super customisable

4 / 14

Using ggplot()

  • Built layer by layer +
  • Variables are mapped onto elements of the plot as 'aesthetics'
    • Use aes() to include variable(s) as an aesthetic
  • Geoms are 'visual marks' that represent data points e.g.

    • geom_bar() – creates a layer of bars
    • geom_point() – creates a layer of data points
    • geom_histogram() – creates a layer with a histogram
    • geom_text() – creates layer with text on

& many more options...

5 / 14

General Process

  1. Create base layer by specifying the data & the variables to map onto the plot

  2. Add geom layer (to display our data points)

  3. Add/edit visual properties (scale, colours, shapes etc.)

  4. Add semantic properties (e.g. title, labels)

  5. Add a theme to make our plot pretty







Today we're going to follow this process, to build different types of common graphs!

6 / 14

Basic Boxplot

peng_boxplot <- ggplot2::ggplot(peng_data, aes(island, bill_length_mm))
peng_boxplot +
geom_boxplot() +
labs(title = "Penguin Bill Length Split by Island", x = "Island", y = "Bill Length (mm)") +
theme_classic()




Task:

Create a boxplot of peng_data with sex on the x axis & flipper_length_mm on the y axis

7 / 14

Grouped Boxplot

peng_boxplot_2 <- ggplot2::ggplot(peng_data, aes(island, bill_length_mm, fill = sex))
peng_boxplot_2 +
geom_boxplot() +
labs(title = "Penguin Bill Length Split by Island and Sex", x = "Island", y = "Bill Length (mm)", fill = "Sex") +
theme_minimal()



Task:

Create a grouped boxplot of peng_data with sex on the x axis, flipper_length_mm on the y axis, & grouped by species

8 / 14

Scatterplots

peng_scatter <- ggplot2::ggplot(peng_data, aes(bill_length_mm, bill_depth_mm))
peng_scatter +
geom_point() +
labs(title = "Scatterplot of Bill Length & Bill Depth", x = "Bill Length (mm)", y = "Bill Depth (mm)") +
theme_bw()

peng_scatter_2 <- ggplot2::ggplot(peng_data, aes(bill_length_mm, bill_depth_mm))
peng_scatter_2 +
geom_point(colour = "dark blue", size = 3, shape = 20, alpha = .5) +
labs(title = "Scatterplot of Bill Length & Bill Depth", x = "Bill Length (mm)", y = "Bill Depth (mm)") +
theme_bw()



Task:

Create a scatterplot of peng_data with body_mass_g on the x axis, flipper_length_mm on the y axis

9 / 14

Means Plots

peng_means <- ggplot2::ggplot(peng_data, aes(species, flipper_length_mm))
peng_means +
stat_summary(fun = "mean", geom = "point", size = 4, colour = "pink") +
labs(title = "Mean Flipper Length per Penguin Species", x = "Species", y = "Flipper Length (mm)") +
theme_minimal()



Task:

Create a means plot of peng_data with island on the x axis, body_mass_g on the y axis

10 / 14

Don't Be Misleading!


11 / 14

Changing the Coordinate System

peng_means <- ggplot2::ggplot(peng_data, aes(species, flipper_length_mm))
peng_means +
stat_summary(fun = "mean", geom = "point", size = 4, colour = "pink") +
labs(title = "Mean Flipper Length per Penguin Species", x = "Species", y = "Flipper Length (mm)") +
coord_cartesian(ylim = c(0, 250)) +
scale_y_continuous(breaks = seq(0, 250, 50)) +
theme_minimal()


Task:

Using the means plot in the previous task, alter the y axis & add appropriate scale breaks (use your judgement here for what would be appropriate)

12 / 14

General Tips

Think about what makes a good graph when creating your own:

  • Clear & tidy
  • Simple – presents a lot with a little!
  • Coherent
  • Accurate – doesn’t distort results
  • Informative captions/titles & labels
  • Is referred to in the text ("see Figure 1")

To Avoid Problems

  • Run & inspect every change you make
  • Be aware that creating plots with ggplot is often a long process
  • A really common error is forgetting the + for each layer
  • Set code chunk options to echo = FALSE (plot is displayed but the code isn't)
13 / 14

Made with Padlet
14 / 14

Session Objectives

Data Visualisation with ggplot2

  • Introduction to ggplot2
  • Boxplot
  • Grouped Boxplot
  • Scatterplot
  • Means plot
2 / 14
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow