How to create ggplot labels in R

Annotate ggplot with text labels using built-in functions and create non-overlapping labels with the ggrepel package.

1 2 Page 2
Page 2 of 2

Code to create data used in this demo:

ma_data <- data.frame(
stringsAsFactors = FALSE,
Place = c("Barnstable","Berkshire","Bristol",
"Dukes and Nantucket","Essex","Franklin",
"Hampden","Hampshire","Middlesex",
"Norfolk","Plymouth","Suffolk",
"Worcester"),
AdultPop = c(165336L,92946L,390230L,20756L,
538981L,53210L,316312L,99377L,1116442L,
488612L,355335L,546850L,564408L),
Bachelors = c(70795L,31034L,109080L,9769L,212106L,
19786L,85913L,46210L,616179L,258768L,
130354L,244827L,202881L),
PctBachelors = c(0.428188658,0.333892798,0.279527458,
0.470659087,0.393531497,0.371847397,
0.271608412,0.464996931,0.551913131,
0.529598127,0.366848186,0.447704124,
0.359458052),
CovidPer100K = c(7,9,30.8,25.3,29.5,4.7,28.1,10.4,
16.7,13.9,14.5,27.4,20),
Positivity = c(0.0188,0.0095,0.0457,0.0294,0.0406,
0.0052,0.0446,0.0063,0.0165,0.0184,
0.0288,0.0171,0.0251),
Region = c("Southeast","West","Southeast",
"Southeast","Northeast","West","West",
"West","MetroBoston","MetroBoston",
"Southeast","MetroBoston","Central")
)
ma_data_fake <- data.frame(
stringsAsFactors = FALSE,
Place = c("Barnstable","Berkshire","Bristol",
"Dukes and Nantucket","Essex","Franklin",
"Hampden","Hampshire","Middlesex",
"Norfolk","Plymouth","Suffolk",
"Worcester","Fake"),
AdultPop = c(165336L,92946L,390230L,20756L,
538981L,53210L,316312L,99377L,1116442L,
488612L,355335L,546850L,564408L,1106400L),
Bachelors = c(70795L,31034L,109080L,9769L,212106L,
19786L,85913L,46210L,616179L,258768L,
130354L,244827L,202881L,610100L),
PctBachelors = c(0.428188658,0.333892798,0.279527458,
0.470659087,0.393531497,0.371847397,
0.271608412,0.464996931,0.551913131,
0.529598127,0.366848186,0.447704124,
0.359458052,0.5394678),
CovidPer100K = c(7,9,30.8,25.3,29.5,4.7,28.1,10.4,
16.7,13.9,14.5,27.4,20,16.3),
Positivity = c(0.0188,0.0095,0.0457,0.0294,0.0406,
0.0052,0.0446,0.0063,0.0165,0.0184,
0.0288,0.0171,0.0251,0.0155),
Region = c("Southeast","West","Southeast",
"Southeast","Northeast","West","West",
"West","MetroBoston","MetroBoston",
"Southeast","MetroBoston","Central",
"MetroBoston")
)
mydf <- structure(list(Rate = c(4.5999999999999996, 4.5, 4.2000000000000002, 
4.2000000000000002, 4.2999999999999998, 4.0999999999999996,
4.0999999999999996, 4.0999999999999996, 4.0999999999999996, 7, 8.9000000000000004,
4.7000000000000002, 4.5999999999999996, 4.2999999999999998, 4.0999999999999996,
4, 3.8999999999999999, 4, 4, 3.7999999999999998, 6.5999999999999996, 8.6999999999999993,
3.7999999999999998, 3.6000000000000001, 3.5, 3.3999999999999999, 3.2000000000000002, 3.1000000000000001,
3, 2.8999999999999999, 3, 6.0999999999999996, 8.0999999999999996, 2.7999999999999998, 2.8999999999999999,
3, 2.7999999999999998, 3.1000000000000001, 3.1000000000000001, 3.2000000000000002, 3.2999999999999998,
3.2000000000000002, 4, 4.2999999999999998), State = c("CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA",
"CA", "CA", "CA", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "MA", "MA", "MA", "MA",
"MA", "MA", "MA", "MA", "MA", "MA", "MA", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE"),
Quarter = c("2018-01-01", "2018-04-01", "2018-07-01", "2018-10-01", "2019-01-01", "2019-04-01", "2019-07-01",
"2019-10-01", "2020-01-01", "2020-04-01", "2020-07-01", "2018-01-01", "2018-04-01", "2018-07-01", "2018-10-01",
"2019-01-01", "2019-04-01", "2019-07-01", "2019-10-01", "2020-01-01", "2020-04-01", "2020-07-01", "2018-01-01",
"2018-04-01", "2018-07-01", "2018-10-01", "2019-01-01", "2019-04-01", "2019-07-01", "2019-10-01", "2020-01-01",
"2020-04-01", "2020-07-01", "2018-01-01", "2018-04-01", "2018-07-01", "2018-10-01", "2019-01-01", "2019-04-01",
"2019-07-01", "2019-10-01", "2020-01-01", "2020-04-01", "2020-07-01")), row.names = c(NA, -44L), class = "data.frame")
 

Copyright © 2020 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform