<- 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
20 geom_t
20.1 geom_text
Package
ggplot2 (Wickham 2016)
Description
Labeling plots.
Understandable aesthetics
required aesthetics
x
, y
optional aesthetics
stat
, position
, size
Example
20.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
20.3 geom_tile
Package
ggplot2 (Wickham 2016)
Description
Create heat map plot. geom_rect()
and geom_tile()
do the same thing, but function inputs are different: geom_rect()
uses the locations of the four corners (xmin, xmax, ymin and ymax), while geom_tile()
uses the center of the tile and its dimensions (x, y, width, height).
Understandable aesthetics
required aesthetics
x
y
optional aesthetics
alpha
, colour
, group
, linetype
, linewidth
See also
Example
|>
worldbankdata filter(Region == "South Asia" ) |>
ggplot(aes(x=Year,
y=Country, fill=Cooking)) +
geom_tile(aes(width=1, height=1), col="white") +
::scale_fill_viridis() +
viridiscoord_fixed()
20.4 geom_text_cooks
Package
ggxmean (Reynolds 2024)
Description
Returns a scatter plot with points that are labeled.
Understandable aesthetics
required aesthetics
x
, y
optional aesthetics
position
, size
, digits
, check_overlap
See also
geom_scatter, geom_text, geom_label
Example
library(ggxmean)
<- worldbankdata |>
p1 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_text_cooks(check_overlap = TRUE, digits = 1) + ggtitle("geom_text_cooks only")
<- worldbankdata |>
p2 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_point(col="red") +
geom_text_cooks(check_overlap = TRUE, digits = 1) + ggtitle("geom_point and geom_text_cooks")
|p2 p1
20.5 geom_text_coordinate
Package
ggplot2 (Wickham 2016)
Description
Returns a scatter plot with points that are labeled with (x, y) coordinates.
Understandable aesthetics
required aesthetics
x
y
optional aesthetics
position
, size
, check_overlap
, nudge_x
See also
geom_text, geom_text_cooks, geom_text_repel
Example
<- worldbankdata |>
p1 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_text_coordinate() +
ggtitle("geom_text_coordinate only")
<- worldbankdata |>
p2 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_point(col="red") +
geom_text_coordinate() +
ggtitle("geom_point and geom_text_coordinate")
|p2 p1
20.6 geom_text_leverage
Package
ggxmean (Reynolds 2024)
Description
Returns a scatter plot with points that are labeled.
Understandable aesthetics
required aesthetics
x
, y
optional aesthetics
position
, size
, check_overlap
, nudge_x
See also
geom_text, geom_text_cooks, geom_text_repel
Example
<- worldbankdata |>
p1 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_text_leverage() +
ggtitle("geom_text_leverage only")
<- worldbankdata |>
p2 filter(Country == "Bolivia" & Cooking > 75) |>
ggplot(aes(x=Cooking, y=Electricity)) +
geom_point(col="red") +
geom_text_leverage() +
ggtitle("geom_point and geom_text_leverage")
|p2 p1