Today we will review the elements of the grammar of graphics and extend this grammar to allow for manual annotations of data visualizations.
For today’s notes, we will once again use the
Here is a plot showing several of the ways that layers and aesthetics can be used to construct an informative visualization:
I have changed the size of the points to scale with the population, but the color of the points is set to a fixed value.
As you saw in
lab08.Rmd, 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.
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
For each annotation, simply add another layer. The exact syntax differs
based on whether we want a point:
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
We will, once again, work on a lab for the remainder of the class: lab09.Rmd Upload your script and html file to GitHub ahead of the next class.