Aesthetics

When creating a ggplot2 object two pieces of information are vital:

  • Input data: A tibble/data.frame in a long format.
  • Aesthetics: Specifies which columns of the input data are used for the different parts of the plot.

This page shows different aesthetics and their uses whilst the previous page described the input data.

Aesthetics

The most important aesthetics are:

  • x: X-axis (continuos or categorical).
  • y: Y-axis (continuos or categorical).
  • shape: The shape of points (categorical). There are 20 different shapes.
  • colour: The colour of objects (continuos or categorical). If used for points this is the stroke colour (i.e. outside line).
  • fill: The fill colour of objects (continuos or categorical).
  • size: The size of shapes (continuos).
  • linetype: The type of the lines (categorical). There are 6 different line types. These include solid, dashed, dotted etc.
  • linewidth: The width of the lines (continuos).

As you can see there are a lot of aesthetic options. Which you use will depends on your data and how you want to visualise it. Below are a few examples of using the various aesthetics.

Full list: Ggplot2 aesthetic specifications.

We’ll create a few scatterplots, chaging the appearance of the points for different reasons.

Dataset

For demonstration we’ll load the crop_and_soil_tbl data from the mgrtibbles package (hyperlink includes install instructions). This contains:

  • Three categorical columns: Soil_type, Crop_type, and Fertilisier.
  • Five continuos column: Humidity, Moisture, Nitrogen, Potassium, and Phosphorus.
  • A combine column: The Temperature_clesius_kelvin column contains the celsius and kelvin values. This will be split into 2 columns.
crop_and_soil_tbl <- mgrtibbles::crop_and_soil_tbl |> 
    #Separate wider
    tidyr::separate_wider_delim(Temperature_celsius_kelvin, delim="_",
    names = c("Temp_celsius", "Temp_kelvin")) |>
    #Mutate across the two columns to convert them to numeric columns
    dplyr::mutate(dplyr::across(Temp_celsius:Temp_kelvin, as.numeric)) |>
    #Slice first 200 rows
    slice(1:50)
#Glimpse tibble
crop_and_soil_tbl |> dplyr::glimpse()
Rows: 50
Columns: 10
$ Soil_type    <chr> "Sandy", "Loamy", "Black", "Red", "Clayey", "Sandy", "Red…
$ Crop_type    <chr> "Maize", "Sugarcane", "Cotton", "Tobacco", "Paddy", "Barl…
$ Fertiliser   <chr> "Urea", "DAP", "14-35-14", "28-28", "Urea", "17-17-17", "…
$ Temp_celsius <dbl> 26, 29, 34, 32, 28, 26, 25, 33, 30, 29, 27, 31, 25, 32, 2…
$ Temp_kelvin  <dbl> -247, -244, -239, -241, -245, -247, -248, -240, -243, -24…
$ Humidity     <dbl> 52, 52, 65, 62, 54, 52, 50, 64, 60, 58, 54, 62, 50, 62, 5…
$ Moisture     <dbl> 38, 45, 62, 34, 46, 35, 64, 50, 42, 33, 28, 48, 65, 41, 3…
$ Nitrogen     <dbl> 37, 12, 7, 22, 35, 12, 9, 41, 21, 9, 13, 14, 36, 24, 14, …
$ Potassium    <dbl> 0, 0, 9, 0, 0, 10, 0, 0, 0, 7, 0, 15, 0, 0, 0, 13, 0, 0, …
$ Phosphorous  <dbl> 0, 36, 30, 20, 0, 13, 10, 0, 18, 30, 40, 12, 0, 22, 41, 1…

Categorical mappings

We’ll create a scatterplot for Humidity (x) against Moisture (y). On top of the x and y aesthetics we’ll set the shape to the Crop_type and the colour to the Soil_type to include categorical data in our plot.

crop_and_soil_tbl |>
    ggplot2::ggplot(aes(x = Humidity, y = Moisture, 
                    colour = Crop_type, shape = Soil_type)) +
        ggplot2::geom_point()

You’ll notice corresponding legends appear which is handy.

Continuous

Rather than mapping categorical values to extra aesthetics we can mapp continuous values.

Create a reate a scatterplot for Humidity (x) against Moisture (y). Additonally map:

  • Temp_celsius to color.
  • Nitrogen to size.
crop_and_soil_tbl |>
    ggplot2::ggplot(aes(x = Humidity, y = Moisture, 
                        color = Temp_celsius, size = Nitrogen)) +
        ggplot2::geom_point()

Correct use of aesthetics

There are many considerations when choosing which aesthetics to use including:

  • How many different aesthetics can be used before the plot is too noisy.
  • Some aesthetics can only be used for continuous or categorical whilst others can be used for both.
  • How many categorical gorupings can be effectively used for an aesthetic. Although you could use 100 colours for 100 groups, the colours will be very hard to differentiate between.
  • Should you be using colour blind friendly palettes?

Some of these will be touched upon in this website. However, if you want more theory and examples I woudl recommend