4.3 Making a Line Graph with Multiple Lines

4.3.1 Problem

You want to make a line graph with more than one line.

4.3.2 Solution

In addition to the variables mapped to the x- and y-axes, map another (discrete) variable to colour or linetype, as shown in Figure 4.6:

A variable mapped to colour (left); A variable mapped to linetype (right)A variable mapped to colour (left); A variable mapped to linetype (right)

Figure 4.6: A variable mapped to colour (left); A variable mapped to linetype (right)

4.3.3 Discussion

The tg data has three columns, including the factor supp, which we mapped to colour and linetype:

Note

If the x variable is a factor, you must also tell ggplot to group by that same variable, as described below.

Line graphs can be used with a continuous or categorical variable on the x-axis. Sometimes the variable mapped to the x-axis is conceived of as being categorical, even when it’s stored as a number. In the example here, there are three values of dose: 0.5, 1.0, and 2.0. You may want to treat these as categories rather than values on a continuous scale. To do this, convert dose to a factor (Figure 4.7):

Line graph with continuous x variable converted to a factor

Figure 4.7: Line graph with continuous x variable converted to a factor

Notice the use of group = supp. Without this statement, ggplot won’t know how to group the data together to draw the lines, and it will give an error:

Another common problem when the incorrect grouping is used is that you will see a jagged sawtooth pattern, as in Figure 4.8:

A sawtooth pattern indicates improper grouping

Figure 4.8: A sawtooth pattern indicates improper grouping

This happens because there are multiple data points at each y location, and ggplot thinks they’re all in one group. The data points for each group are connected with a single line, leading to the sawtooth pattern. If any discrete variables are mapped to aesthetics like colour or linetype, they are automatically used as grouping variables. But if you want to use other variables for grouping (that aren’t mapped to an aesthetic), they should be used with group.

Note

When in doubt, if your line graph looks wrong, try explicitly specifying the grouping variable with group. It’s common for problems to occur with line graphs because ggplot is unsure of how the variables should be grouped.

If your plot has points along with the lines, you can also map variables to properties of the points, such as shape and fill (Figure 4.9):

Line graph with different shapes (left); With different colors (right)Line graph with different shapes (left); With different colors (right)

Figure 4.9: Line graph with different shapes (left); With different colors (right)

Sometimes points will overlap. In these cases, you may want to dodge them, which means their positions will be adjusted left and right (Figure 4.10). When doing so, you must also dodge the lines, or else only the points will move and they will be misaligned. You must also specify how far they should move when dodged:

Dodging to avoid overlapping points

Figure 4.10: Dodging to avoid overlapping points