22  geom_t

22.1 geom_text

Package

ggplot2 (Wickham 2016)

Description

Labeling plots.

Understandable aesthetics

required aesthetics

x, y

optional aesthetics

stat , position , size

Example

p1 <- drop_na(worldbankdata) |>
  filter(Code %in% c('AFG', 'AGO', 'BEN', 'BFA', 'BGD', 'BOL', 'BWA')) |>
  ggplot(aes(x = Cooking, y = Electricity, label = Income)) + geom_text(size = 3) + scale_color_brewer(palette = "Dark2") + scale_fill_brewer(palette = "Dark2") + ggtitle("geom_text only")
p2 <- drop_na(worldbankdata) |>
  filter(Code %in% c('AFG', 'AGO', 'BEN', 'BFA', 'BGD', 'BOL', 'BWA')) |>
  ggplot(aes(x = Cooking, y = Electricity, label = Income)) + 
  geom_point(aes(color = Income)) +
  geom_text(size = 3) + scale_color_brewer(palette = "Dark2") + scale_fill_brewer(palette = "Dark2") + ggtitle("geom_point and geom_text only")
p1|p2

22.2 geom_text_repel

Package

ggrepel (Slowikowski 2024)

Description

Repulsive textual annotations.

Understandable aesthetics

required aesthetics

`x, y

optional aesthetics

stat , position , size

Example

library(ggrepel)
p1 <- ggplot(mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
  geom_text_repel() +  
  labs(title = "",x = "Weight (1000 lbs)", y = "Miles/(US) gallon") + scale_color_brewer(palette = "Dark2") + scale_fill_brewer(palette = "Dark2") + ggtitle("geom_text_repel only")
p2 <- ggplot(mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
  geom_point() +
  geom_text_repel() +  
  labs(title = "",x = "Weight (1000 lbs)", y = "Miles/(US) gallon") + scale_color_brewer(palette = "Dark2") + scale_fill_brewer(palette = "Dark2") + ggtitle("geom_text_repel and geom_point")
p1|p2

Slowikowski, Kamil. 2024. Ggrepel: Automatically Position Non-Overlapping Text Labels with ’Ggplot2’. https://CRAN.R-project.org/package=ggrepel.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.