4.2 Adding Points to a Line Graph

4.2.1 Problem

You want to add points to a line graph.

4.2.2 Solution

Add geom_point() (Figure 4.4):

ggplot(BOD, aes(x = Time, y = demand)) +
  geom_line() +
  geom_point()
Line graph with points

Figure 4.4: Line graph with points

4.2.3 Discussion

Sometimes it is useful to indicate each data point on a line graph. This is helpful when the density of observations is low, or when the observations do not happen at regular intervals. For example, in the BOD data set there is no entry for Time=6, but this is not apparent from just a bare line graph (compare Figure 4.3 with Figure 4.4).

In the worldpop data set, the intervals between each data point are not consistent. In the far past, the estimates were not as frequent as they are in the more recent past. Displaying points on the graph illustrates when each estimate was made (Figure 4.5):

library(gcookbook) # Load gcookbook for the worldpop data set

ggplot(worldpop, aes(x = Year, y = Population)) +
  geom_line() +
  geom_point()

# Same with a log y-axis
ggplot(worldpop, aes(x = Year, y = Population)) +
  geom_line() +
  geom_point() +
  scale_y_log10()
Top: points indicate where each data point is; bottom: the same data with a log y-axisTop: points indicate where each data point is; bottom: the same data with a log y-axis

Figure 4.5: Top: points indicate where each data point is; bottom: the same data with a log y-axis

With the log y-axis, you can see that the rate of proportional change has increased in the last thousand years. The estimates for the years before 0 have a roughly constant rate of change of 10 times per 5,000 years. In the most recent 1,000 years, the population has increased at a much faster rate. We can also see that the population estimates are much more frequent in recent times–and probably more accurate!

4.2.4 See Also

To change the appearance of the points, see Recipe 4.5.