As far as I can see ggplot2
knows the dimensions of labels plotted by geom_text
. Otherwise the check_overlap
option would not work.
Where are these dimensions stored and how can I access them?
Illustrative example
library(ggplot2)
df <- data.frame(x = c(1, 2),
y = c(1, 1),
label = c("label-one-that-might-overlap-another-label",
"label-two-that-might-overlap-another-label"),
stringsAsFactors = FALSE)
With check_overlap = FALSE
(the default), the labels overplot each other.
ggplot(df, aes(x, y)) +
geom_text(aes(label = label)) +
xlim(0, 3)
With check_overlap = TRUE
, the second label is not plotted, because ggplot
finds an overlap.
ggplot(df, aes(x, y)) +
geom_text(aes(label = label), check_overlap = TRUE) +
xlim(0, 3)
How does ggplot2
know that those labels overlap? How can I access that information?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…