Tin Le Mini Lesson 7 geom_label & geom_text

Loading tidyverse and based mtcars

library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
cars <- head(mtcars, 25)

add text with geom_text

ggplot(cars, aes(wt, mpg)) +
  geom_point() +
  geom_text(label = rownames(cars), nudge_x = 0.25, nudge_y = 0.25, check_overlap = TRUE)

ggplot(cars, aes(wt, mpg, label = rownames(cars))) +
  geom_text(check_overlap = TRUE)

to improve readability, you would use geom_label

ggplot(cars, aes(wt, mpg)) +
  geom_point() +
  geom_label(label = rownames(cars), nudge_x = 0.25, nudge_y = 0.2)

Adding another attribute to geom_label using fill wihting ggplot

ggplot(cars, aes(x = wt, y =  mpg, fill = cyl)) +
  geom_label(label= rownames(cars), nudge_x = 0.25, nudge_y = 0.2, color = "white", size = 3)