小编Rav*_*avi的帖子

ggrepel:结合使用 position_dodge 和 geom_label_repel

我试图在geom_boxplotusing 中标记异常值ggrepel::geom_label_repel。当只有一个分组变量时它工作得很好,但是当我尝试将它用于多个分组变量时,我遇到了问题。由于某种原因,ggrepel 中的 position 参数似乎不太一致,请参见此示例:

library(tidyverse)
library(ggrepel)

set.seed(1337)

df <- tibble(x = rnorm(500),
             g1 = factor(sample(c('A','B'), 500, replace = TRUE)),
             g2 = factor(sample(c('A','B'), 500, replace = TRUE)),
             rownames = 1:500)

is_outlier <- function(x) {
    return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}

df_outliers <- df %>% group_by(g1, g2) %>% mutate(outlier=is_outlier(x))

ggplot(df_outliers, aes(x=g1, y=x, fill=g2)) + 
    geom_boxplot(width=0.3, position = position_dodge(0.5)) +
    ggrepel::geom_label_repel(data=. %>% filter(outlier), 
                              aes(label=rownames), position = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggrepel

5
推荐指数
1
解决办法
838
查看次数

标签 统计

ggplot2 ×1

ggrepel ×1

r ×1