Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Know your problem
Know your data
Visualize your data – numerical variables
Work with your chart
16. Chart aesthetics: adding country names
Check yourself

Instruction

To highlight data context, add labels to the plot.

The main goal of the scatter plot is to show trends, not specific points. To detail-oriented readers, scatter plots can appear like nothing more than a bunch of dots. To make the scatter plot more detailed, interesting, and understandable, we can highlight some distinctive points.

What will we highlight? There are several possibilities, including:

  • Extreme points – The minimum and maximum of both variables
  • Untypical points – One or more "outlier" points that stand out from the main bunch
  • Specific points – One or more points that add context to the data

In this case, we can focus on one particular country and highlight it on the chart by changing its point color and adding a label. To do that, we'll use two geometry commands: geom_text and geom_point.

First, add the country name to the data point using geom_text:

+ geom_text( data = subset(alcohol_wealth, country == "Spain"), aes(label=country), color="red", nudge_x = 0.1, nudge_y =-0.5 )

We've annotated the data for Spain, putting its name (enclosed in double quotes) in the data argument:

subset(alcohol_wealth, country == "Spain")

This will choose rows in the alcohol_wealth dataset where the country is equal to "Spain". The rest of the arguments change the color of the text and put it under the point.

Next, we'll change the color of the corresponding dot. To do that, add the following command:

+ geom_point( data = subset(alcohol_wealth, country == "Spain"), color="red")

This uses similar syntax to highlight the appropriate row.

Exercise

Highlight Germany in our plot. Add the functions discussed above to change the dot color and add a label to the data point.

When you're done, press the Run and Check Code button to check your code.

Stuck? Here's a hint!

You should write:

geom_text(data = subset(alcohol_wealth, country == "Germany"),
  aes(label = country), color = "red", nudge_x = 0.1, nudge_y = -0.5) + 
  geom_point(data = subset(alcohol_wealth, country == "Germany"),
  color = "red")