我怎样才能从 ggplot2 的情节中排斥标签名称
How can I repel the label name from the plot in ggplot2
我有一个箱线图,我想在其中显示标签名称。问题是我想排斥这些标签名称,使它们超出箱线图。我尝试了 ggrepel
包中的 geom_text_repel
,但当它们相互重叠时它会排斥标签。
也试过这样的建议:Repel geom label and text in ggplot. And ordering geom points based on size
并且没有收到任何关于解决我的问题的全面信息。
样本:
mtdata <- mtcars %>%
rownames_to_column(var = "name") %>%
mutate(cyl = as.factor(cyl))
ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
geom_text_repel(data = mtdata %>%
filter(mpg > 20 & wt >3), aes(label = name))
理想的输出:
所以,你可以看到有一个点描述了准确的标签位置和排斥。
厚脸皮的解决方案是简单的推动。
library(tidyverse)
mtdata <- mtcars %>%
rownames_to_column(var = "name") %>%
mutate(cyl = as.factor(cyl))
ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
ggrepel::geom_text_repel(data = mtdata %>%
filter(mpg > 20 & wt >3), aes(label = name), nudge_x = .75)
由 reprex package (v0.3.0)
于 2021-02-08 创建
我有一个箱线图,我想在其中显示标签名称。问题是我想排斥这些标签名称,使它们超出箱线图。我尝试了 ggrepel
包中的 geom_text_repel
,但当它们相互重叠时它会排斥标签。
也试过这样的建议:Repel geom label and text in ggplot. And ordering geom points based on size
并且没有收到任何关于解决我的问题的全面信息。
样本:
mtdata <- mtcars %>%
rownames_to_column(var = "name") %>%
mutate(cyl = as.factor(cyl))
ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
geom_text_repel(data = mtdata %>%
filter(mpg > 20 & wt >3), aes(label = name))
理想的输出:
所以,你可以看到有一个点描述了准确的标签位置和排斥。
厚脸皮的解决方案是简单的推动。
library(tidyverse)
mtdata <- mtcars %>%
rownames_to_column(var = "name") %>%
mutate(cyl = as.factor(cyl))
ggplot(mtdata, aes(x = cyl, y = mpg)) + geom_boxplot() +
ggrepel::geom_text_repel(data = mtdata %>%
filter(mpg > 20 & wt >3), aes(label = name), nudge_x = .75)
由 reprex package (v0.3.0)
于 2021-02-08 创建