<- drop_na(worldbankdata) |>
p1 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")
<- drop_na(worldbankdata) |>
p2 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")
|p2 p1
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
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)
<- ggplot(mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
p1 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")
<- ggplot(mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
p2 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")
|p2 p1
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.