Session details

Objectives

  1. To become aware of the powerful features of ggplot2.
  2. To learn about some of the fundamentals of easily creating amazing graphics.
  3. To know about some resources to continue learning.

At the end of this session, you will achieve this objective by creating a fairly simple, visually-appealing graph that shows:

  • At least three data values, ie. “aesthetics” or aes(), such as what to put on the x-axis, the y-axis, and or using colour or size.
  • At least two layers, ie “geometries” or geom_, such as points, lines, or boxplots.
  • That has clearly labelled x and y axes, ie. labs().
  • That has some changes to the look and feel of the plot, ie. theme(), so that it is publication ready.

Quickly get familiar with data to plot

For this session we will be using the CO2 dataset. Here is some code to get a sense of the data.

Exercise: Choose a dataset and check it out

There are several exercises in this session. Choose one of the below datasets and use that dataset for all later exercises.

For complete R beginners, use:

  • mpg

For more confident R users, use one of these:

  • economics
  • diamonds
  • msleep
  • txhousing

Check out the contents of the dataset you choose using:

Basic structure of using ggplot2

ggplot2 uses the “Grammar of Graphics” (gg). This is a powerful approach to creating plots because it provides a consistent way of telling ggplot2 what to do. There are at least three aspects to using ggplot2 that relate to the grammar:

  • Aesthetics, aes(): How data should be mapped to the plot. Includes what to put on x axis, on the y axis, colours, size, etc.
  • Geometries, geom_: The visual representation of the data, as a layer. This tells ggplot2 how to show the aesthetics. Includes points, lines, boxes, etc.
  • Themes, theme_ or theme(): How the plot should look like. Includes the text, axis lines, etc.

To maximise the power of ggplot2, make heavy use of autocompletion. You can do this by typing, for instance, geom_ and then hitting the TAB key to see a list of all the geoms. Or after typing theme(, hit TAB to see all the options inside theme.

Visualise 1-dimensional (x axis) data

There are many ways of showing plotting continuous (e.g. weight, height) variables in ggplot2. For discrete (e.g. terrain type: mountain, plains, or sex: woman, man) variables, there is really only one way.

Visualise 2-dimensional (x and y axis) data

You can of course include data on the y axis too! This is usually what you use graphs for! There are many more types of “geoms” to use for having data on both axes, and which one you choose depends on what you are trying to show and what the data is like. Usually you put the variable that you can influence (the independent variable) on the x axis and the variable that responds (the dependent variable) on the y axis.

Using a third (or more) variable

You can also add an additional dimension to the data by using other elements (colours, size, transparency, etc) of the graph to represent another variable. This is NOT the same thing as using 3-dimensionl (aka x, y, z axis) plots, which should be avoided unless absolutely necessary! Using colours to represent discrete groups is useful, or for using shading to represent a range in continuous values.

Or add a fourth variable.

And it’s easy to add another geoms!

Axis titles and the theme

Let’s get to making the plot prettier. There are many many options to customise the plot using the theme().

Saving the plot

Now, if you want to save the plot, you can do that pretty easily!

Exercise: Putting it all together

Time: Until end of session

  1. Create a ggplot, choosing three variables for the aes(), one for:
    • the x-axis
    • the y-axis
    • either size, colour, alpha, stroke, or fill
  2. Create two geom_ layers. The geom you use will depend on the variables and the specific aes() you choose above.
  3. Properly label the x and y axis with labs().
  4. Choose a pre-defined theme (theme_) and make two changes to it using theme().
  5. Save the plot with ggsave().

This work is licensed under a Creative Commons Attribution 4.0 International License. See the licensing page for more details about copyright information.