2.4 Aesthetic Mappings
The aesthetic mappings take properties of the data and use them to influence visual characteristics, such as position, color, size, shape, or transparency. Each visual characteristic can thus encode an aspect of the data and be used to convey information.
All aesthetics for a plot are specified in the aes() function call (later in this tutorial you will see that each geom
layer can have its own aes
specification). For example, we can add a mapping from the class of the cars to a color characteristic:
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
geom_point()
Using the aes()
function will cause the visual channel to be based on the data specified in the argument. For example, using aes(color = "blue")
won’t cause the geometry’s color to be “blue”, but will instead cause the visual channel to be mapped from the vector c("blue")
— as if we only had a single type of engine that happened to be called “blue”. If you wish to apply an aesthetic property to an entire geometry, you can set that property as an argument to the geom
method, outside of the aes()
call:
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "blue")