12.2 Representing Variables with Colors

12.2.1 Problem

You want to use a variable (column from a data frame) to control the color of geoms.

12.2.2 Solution

In the call to the geom, inside of aes(), set the value of colour or fill to the name of one of the columns in the data (Figure 12.2):

Mapping a variable to fill (left); Mapping a variable to colour for points (right)Mapping a variable to fill (left); Mapping a variable to colour for points (right)

Figure 12.2: Mapping a variable to fill (left); Mapping a variable to colour for points (right)

When the mapping is specified in ggplot() it is used as the default mapping, which is inherited by all the geoms. Within a geom, the default mappings can be overridden.

12.2.3 Discussion

In the cabbage_exp example, the variable Cultivar is mapped to fill. The Cultivar column in cabbage_exp is a factor, so ggplot treats it as a categorical variable. You can check the type using str():

In the mtcars example, cyl is numeric, so it is treated as a continuous variable. Because of this, even though the actual values of cyl include only 4, 6, and 8, the legend has entries for the intermediate values 5 and 7. To make ggplot treat cyl as a categorical variable, you can convert it to a factor in the call to ggplot() (Figure 12.3, left), or you can modify the data so that the column is a character vector or factor (Figure 12.3, right):

Converting cyl to a factor, within the call to ggplot (left); By modifying the dataframe (right)Converting cyl to a factor, within the call to ggplot (left); By modifying the dataframe (right)

Figure 12.3: Converting cyl to a factor, within the call to ggplot (left); By modifying the dataframe (right)

12.2.4 See Also

You may also want to change the colors that are used in the scale. For continuous data, see Recipe 12.6. For discrete data, see Recipe 12.4 and Recipe 12.5.