据我ggplot2 所知,由绘制的标签的尺寸geom_text。否则,该check_overlap选项将不起作用。
这些维度存储在哪里,如何访问它们?
说明性例子
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)
Run Code Online (Sandbox Code Playgroud)
使用check_overlap = FALSE(默认)标签会相互重叠。
ggplot(df, aes(x, y)) +
geom_text(aes(label = label)) +
xlim(0, 3)
Run Code Online (Sandbox Code Playgroud)
使用check_overlap = TRUE,不会绘制第二个标签,因为会ggplot发现重叠。
ggplot(df, aes(x, y)) +
geom_text(aes(label = label), check_overlap = TRUE) +
xlim(0, 3)
Run Code Online (Sandbox Code Playgroud)
怎么ggplot2知道那些标签重叠?我如何访问该信息?
小智 -1
如果您只是想避免标签重叠,那么 ggrepel 包效果很好。
library(ggplot2)
library(ggrepel)
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)
ggplot(df, aes(x, y)) +
geom_text_repel(aes(label = label), check_overlap = F) +
xlim(0, 3)
Run Code Online (Sandbox Code Playgroud)