Lecture 23

Aesthetic Mappings

Byeong-Hak Choe

SUNY Geneseo

October 30, 2024

Aesthetic Mappings

Aesthetic Mappings

  • In the plot above, one group of points (highlighted in red) seems to fall outside of the linear trend.

    • How can you explain these cars? Are those hybrids?

Aesthetic Mappings

  • An aesthetic is a visual property (e.g., size, shape, color) of the objects (e.g., class) in your plot.

  • You can display a point in different ways by changing the values of its aesthetic properties.

Aesthetic Mappings

Adding a color to the plot

ggplot( data = mpg,
        mapping = 
          aes(x = displ,
              y = hwy, 
              color = class) ) + 
  geom_point()

Aesthetic Mappings

Adding a shape to the plot

ggplot( data = mpg,
        mapping = 
          aes(x = displ,
              y = hwy, 
              shape = class) ) + 
  geom_point()

Aesthetic Mappings

Adding a size to the plot

ggplot( data = mpg,
        mapping = 
          aes(x = displ,
              y = hwy, 
              size = class) ) + 
  geom_point()

Aesthetic Mappings

Specifying a color to the plot, manually

ggplot(data = mpg,
       mapping = 
         aes(x = displ, 
             y = hwy) ) + 
  geom_point(color = "blue")

Aesthetic Mappings

Specifying an color to the plot, manually

ggplot(data = mpg,
       mapping = 
         aes(x = displ, 
             y = hwy) ) + 
  geom_smooth(color = "darkorange") 

Aesthetic Mappings

Specifying an fill to the plot, manually

ggplot(data = mpg,
       mapping = 
         aes(x = displ, 
             y = hwy) ) + 
  geom_smooth(color = "darkorange",
              fill = "darkorange") 

  • In general, each geom_*() has a different set of aesthetic parameters.
    • E.g., fill is available for geom_smooth(), not geom_point().

Aesthetic Mappings

Specifying a size to the plot, manually

ggplot(data = mpg,
       mapping = 
         aes(x = displ, 
             y = hwy) ) + 
  geom_point(size = 3)  # in *mm*

Aesthetic Mappings

Specifying an alpha to the plot, manually

ggplot(data = mpg,
       mapping = 
         aes(x = displ, 
             y = hwy) ) + 
  geom_point(alpha = .3) 

  • We’ve done this to address the issue of overplotting in the scatterplot.

Aesthetic Mappings

Mapping Aesthetics vs. Setting Them Manually

Aesthetic Mapping

  • Links data variables to visible properties on the graph
  • Different categories → different colors or shapes

Setting Aesthetics Manually

  • Customize visual properties directly in geom_*() outside of aes()
  • Useful for setting fixed colors, sizes, or transparency unrelated to data variables