# Class 09: Annotating Data Visualizations

## Learning Objectives

• Review elements in the Grammar of Graphics
• Manually annotate of data visualizations with text, lines, and points

## GapMinder Data

For today’s notes, we will once again use the gapminder_2007 dataset:

## Labels

We can add labels to the plot by adding new layers to the plot:

• xlab("text for the x-axis")
• ylab("text for the y-axis")
• ggtitle("text for the title/top of the plot")
• labs(size = "label for the size legend")

Let’s add these to the current plot:

Do not feel that you need to add complex labels to plots as we are doing an exploratory analysis. However, when presenting plots in a report these should certainly be used to clarify the plot to the audience or readers.

## Annotations

While we can use graphics simply for our own exploratory work, they can often also be used to make visual arguments. That’s the case, for example, whenever we use a graphic in a report or presentation. In order to strengthen a visual argument it is useful to add manual annotations to the plot to help explain our main points.

In order to add manual annotations, we will use the function annotate. For each annotation, simply add another layer. The exact syntax differs based on whether we want a point:

Text:

Or a rect(angle):

For example, to add the current life expectancy (79.1) and GDP (51736) of Virginia onto the plot, I would do this:

Similarly, I could label the US on the plot:

Or, highlight the healthy and wealthy part of the plot:

Notice here that I set the aesthetics fill (that the filled color of the rectangle) and alpha (how opaque the rectangle is); I also put the rectangle first so that the points were plotted behind the rectangle.