Aesthetic mappings

Claus O. Wilke

2024-12-27

Plots map data onto graphical elements

Dataset:
Daily average temperatures for various locations

location day_of_year month temperature
Death Valley 1 01 51.0
Death Valley 2 01 51.2
Death Valley 3 01 51.3
Death Valley 4 01 51.4
Death Valley 5 01 51.6
Death Valley 6 01 51.7
Death Valley 7 01 51.9
Death Valley 8 01 52.0
Death Valley 9 01 52.2
Death Valley 10 01 52.3
Death Valley 11 01 52.5
Death Valley 12 01 52.7
Death Valley 13 01 52.9

Temperatures mapped onto y position

 

Temperatures mapped onto color


 

Commonly used aesthetics

The same data values can be mapped to different aesthetics

We can use many different aesthetics at once

 

Creating aesthetic mappings in ggplot

Getting the data

All examples will use the temperatures dataset:

temperatures <- read_csv("https://wilkelab.org/SDS366/datasets/tempnormals.csv") |>
  mutate(
    location = factor(
      location, levels = c("Death Valley", "Houston", "San Diego", "Chicago")
    )
  ) |>
  select(location, station_id, day_of_year, month, temperature)

We define the mapping with aes()

ggplot(
  data = temperatures,
  mapping = aes(x = day_of_year, y = temperature, color = location)
) + 
  geom_line()

 

We define the mapping with aes()

ggplot(
  data = temperatures,
  mapping = aes(x = day_of_year, y = location, color = temperature)
) + 
  geom_point(size = 5)

 

We frequently omit argument names

Long form, all arguments are named:

ggplot(
  data = temperatures,
  mapping = aes(x = day_of_year, y = location, color = temperature)
) + geom_point(size = 5)


Abbreviated form, common arguments remain unnamed:

ggplot(temperatures, aes(day_of_year, location, color = temperature)) + 
  geom_point(size = 5)

The geom determines how the data is shown

ggplot(temperatures, aes(day_of_year, temperature, color = location)) + 
  geom_line()

 

The geom determines how the data is shown

ggplot(temperatures, aes(day_of_year, location, color = temperature)) + 
  geom_point(size = 5)

 

The geom determines how the data is shown

ggplot(temperatures, aes(month, temperature, color = location)) + 
  geom_boxplot()

 

The geom determines how the data is shown

ggplot(temperatures, aes(month, temperature, fill = location)) + 
  geom_violin() + 
  facet_wrap(~location) # make separate panel per location

 

Important: Distinguish between color and fill

Distinguish between color and fill


color
Applies color to points, lines, text, borders


fill
Applies color to any filled areas

Many geoms use both color and fill

ggplot(temperatures, aes(month, temperature, color = location)) + 
  geom_boxplot()

 

Many geoms use both color and fill

ggplot(temperatures, aes(month, temperature, fill = location)) + 
  geom_boxplot()

 

Many geoms use both color and fill

ggplot(temperatures, aes(month, temperature, color = location, fill = location)) + 
  geom_boxplot()

 

Aesthetics can also be used as parameters

ggplot(temperatures, aes(month, temperature, fill = location)) + 
  geom_boxplot(color = "steelblue")

 

Aesthetics can also be used as parameters

ggplot(temperatures, aes(month, temperature, color = location)) + 
  geom_boxplot(fill = "steelblue")

 

Further reading