4.16 Labels & Annotations (2)
- Figure 4.6 illustrates various geoms for labeling and annotations.
geom_text_repel()
: Automatic label location available from theggrepel
package- See the ggrepel vignette
- Delete labels beforehand in the data (Q: Another solution?)
# data_twitter_influence.csv
data <- read_csv(sprintf("https://docs.google.com/uc?id=%s&export=download",
"1dLSTUJ5KA-BmAdS-CHmmxzqDFm2xVfv6"),
col_types = cols())
data$last_name[data$followers_count<50000] <- NA # Explain!
ggplot(data = data,
aes(x = followers_count,
y = n_retweets,
colour = party)) +
geom_point() +
geom_text_repel(aes(label = last_name, color = party),
segment.color = 'grey50',
size = 3) +
geom_vline(aes(xintercept = mean(followers_count))) +
geom_hline(aes(yintercept = mean(n_retweets))) +
annotate("rect", xmin = 300000, xmax = 700000,
ymin = 30000, ymax = 50000,
color = "black",
fill = "white") +
annotate("text",
label = "Why is no one\n in this area?",
x = 500000, y = 40000,
size = 5,
colour = "red",
hjust = 0.5,
vjust = 0.5)
## Warning: Removed 477 rows containing missing values (geom_text_repel).

Figure 4.6: Labels and annotations