Visualize your data - line chart
Work with your chart
12. Add important value labels on the plot
Check yourself

Instruction

To give context to the data, add some value labels.

Sometimes you need to draw special attention to a few precise values in your data. They might be minimum or maximum values, or they might relate to a specific time or an interesting group. In such a situation, you can add value labels near corresponding points. This will make your reader direct their attention to whatever you want to highlight.

To add value labels, use geom_text() as shown below:

geom_text(aes(x=position_x, y = position_y, label = value_labels), nudge_y=-.5, color="grey")

This command takes three main arguments: x sets the horizontal position of the label, y sets its vertical position, and label sets the displayed text. We have another argument here as well: nudge_y, which nudges the labels in a vertical direction from the line (+0.5 up and -0.5 down). The final argument, color, sets the color of the label's text.

In our line chart, we want to highlight the minimum and maximum values of the consumption variable. The minimum value (1.68 litres per capita) was recorded in 2000 and the maximum (7.27 litres per capita) was recorded in 1984.

Remember that the x axis is now a time axis, so you have to use arguments in date format. Do this by specifying for example parse_date_time(2000, orders='Y').

Exercise

Add labels to the maximum and minimum values in our line chart. Use one instance of geom_text() for each label. Set the x and y arguments according to the proper values (1.68 and 7.27). Set the labels text to these values as well, and apply nudge_y with your choice of + (above the line) or - (below the line).

Stuck? Here's a hint!

You should write:

geom_text(aes(label = 1.68,x = parse_date_time(2000, orders = "Y"),
    y = 1.68),nudge_y = -0.5, color = "grey") + 
geom_text(aes(label = 7.27,x = parse_date_time(1984, orders = "Y"), 
    y = 7.27), nudge_y = 0.5, color = "grey")