4.4 Changing the Appearance of Lines

4.4.1 Problem

You want to change the appearance of the lines in a line graph.

4.4.2 Solution

The type of line (solid, dashed, dotted, etc.) is set with linetype, the thickness (in mm) with size, and the color of the line with colour (or color).

These properties can be set (as shown in Figure 4.11) by passing them values in the call to geom_line():

Line graph with custom linetype, size, and colour

Figure 4.11: Line graph with custom linetype, size, and colour

If there is more than one line, setting the aesthetic properties will affect all of the lines. On the other hand, mapping variables to the properties, as we saw in Recipe 4.3, will result in each line looking different. The default colors aren’t the most appealing, so you may want to use a different palette, as shown in Figure 4.12, by using scale_colour_brewer() or scale_colour_manual():

Using a palette from RColorBrewer

Figure 4.12: Using a palette from RColorBrewer

4.4.3 Discussion

To set a single constant color for all the lines, specify colour outside of aes(). The same works for size, linetype, and point shape (Figure 4.13). You may also have to specify the grouping variable:

Line graph with constant size and color (left); With supp mapped to colour, and with points added (right)Line graph with constant size and color (left); With supp mapped to colour, and with points added (right)

Figure 4.13: Line graph with constant size and color (left); With supp mapped to colour, and with points added (right)

4.4.4 See Also

For more information about using colors, see Chapter 12.