【问题标题】:How to avoid overlapping of labels/texts of boxplot in R?如何避免 R 中箱线图的标签/文本重叠?
【发布时间】:2019-12-07 04:12:28
【问题描述】:

我正在绘制箱线图和小提琴图,以使用ggplot2 查看数据分布。箱线图的四分位数非常接近。这就是为什么它会导致重叠。

我使用了ggrepel::geom_label_repel,但是没有用。如果我删除geom_label_repel,一些标签会重叠。

这是我的 R 代码和示例数据:

dataset <- data.frame(Age = sample(1:20, 100, replace = T))

ggplot(dataset, aes(x = "", y = Age)) +
    geom_violin(position = "dodge", width = 1, fill = "blue") +
    geom_boxplot(width=0.1, position = "dodge", fill = "red") +
    stat_boxplot(geom = "errorbar", width = 0.1) +
    stat_summary(geom = "label", fun.y = quantile, aes(label = ..y..),
                 position = position_nudge(x = -0.05), size = 3) +
    ggrepel::geom_label_repel(aes(label = quantile)) +
    ggtitle("") +
    xlab("") +
    ylab(Age)

除此之外,有没有人熟悉箱线图和小提琴图的组合?左边是箱线图,右边是小提琴图(我不是在问并排的情节。只是一个情节)。

【问题讨论】:

  • library(ggrepel),那么你可以简单地做stat_summary(geom = "label_repel", .....)
  • 谢谢。那行得通。您对问题的第二部分(半箱线图和半小提琴图)有任何想法吗?

标签: r ggplot2 boxplot violin-plot ggrepel


【解决方案1】:

这里的方法略有不同,没有ggrepel。半个小提琴情节其实是经典的密度情节,只是垂直的。这就是剧情的基础。我正在添加带有ggstance::geom_boxploth 的水平箱线图。对于标签,我们不能再使用stat_summary,因为我们不能总结 x 值(也许有人知道怎么做,我不知道)。所以我使用this fantastically obscure code by @eipi10 一次性预先计算了分位数。您可以将箱线图的位置设置为 0,然后只填充密度图,以避免在计算段等时出现一些真正的黑客攻击。

然后,您可以根据自己的喜好巧妙地微调图表。

library(tidyverse)
library(ggstance)
#> 
#> Attaching package: 'ggstance'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     geom_errorbarh, GeomErrorbarh

dataset <- data.frame(Age = sample(1:20, 100, replace = T))

my_quant <- dataset %>% 
  summarise(Age = list(enframe(quantile(Age, probs=c(0.25,0.5,0.75))))) %>% 
  unnest

my_y <- 0

ggplot(dataset) +
  ggstance::geom_boxploth(aes(x = Age, y = my_y), width = .05) +
  geom_density(aes(x = Age)) +
  annotate(geom = "label", x = my_quant$value, my_y, label = my_quant$value) +
  coord_flip()

现在添加填充。


ggplot(dataset) +
  ggstance::geom_boxploth(aes(x = Age, y = my_y), width = .05) +
  geom_density(aes(x = Age), fill = 'white') +
  annotate(geom = "label", x = my_quant$value, my_y, label = my_quant$value) +
  coord_flip()

reprex package (v0.2.1) 于 2019-07-29 创建

【讨论】:

  • 感谢您的回答。它完美地工作。我试图修复密度图的宽度(这可能不合逻辑,但我只是尝试过)以获得始终相同大小的箱线图和密度图;但是,width 选项不适用于geom_density()。由于它不起作用,我从ggstance::geom_boxploth() 中删除了width=0.05。它也不起作用,因为某些变量集中在某些值周围,并且某些变量无法看到箱线图。你知道如何以一个不错的比例排列两个图的宽度,这样我就可以得到一个不错的图?
  • 很高兴我能帮上忙。我不确定我是否完全理解“始终相同大小的 b 图和 d 图”是什么意思?...
  • 由于这个样本是正态分布的,所以看起来很正常。然而,在实际数据中,一些值在变量中占主导地位,假设 90% 集中在 0 附近,其余的均匀分布在其他值周围,密度图的边界远远超过箱线图的宽度。我想看到他们的比例很好。我正在为大约 40 个变量绘制此图并放入 for 循环。我无法控制每个变量的限制。
  • 也许只需尝试geom_density(aes(x = ..., y = ..scaled..)),然后将箱线图宽度设置为 0.5
【解决方案2】:

当使用标准 R boxplot 命令时,使用命令 text 将 5 个统计参数包含到图表中。
示例:

#
boxplot(arq1$J00_J99,arq1$V01_Y89,horizontal = TRUE)
text(x = boxplot.stats(arq1$J00_J99)$stats, labels = 
boxplot.stats(arq1$J00_J99)$stats, y = 0.5)
text(x = boxplot.stats(arq1$V01_Y89)$stats, labels = 
boxplot.stats(arq1$V01_Y89)$stats, y = 2.5)

这显示了标签重叠到上部boxplot 为避免这种情况,请执行两次text,将不同的统计参数选择到不同的 y 高度:

 text(x = boxplot.stats(arq1$V01_Y89)$stats[2:5], labels = 
  boxplot.stats(arq1$V01_Y89)$stats[2:5], y = 2.5)
 text(x = boxplot.stats(arq1$V01_Y89)$stats[1], labels = 
  boxplot.stats(arq1$V01_Y89)$stats[1], y = 2.)


#   

上面我要求包含从 2 到 5 的参数:第一个四分位数、中位数、第三个四分位数和最大值 y=2.5 和最小值 y=2
这解决了任何类型的统计参数重叠到箱线图中

【讨论】:

    【解决方案3】:

    在使用标准的R boxplot命令时,使用命令text将5个统计参数包含到图中,例如:

    boxplot(arq1$J00_J99,arq1$V01_Y89,horizontal = TRUE)
    text(x = boxplot.stats(arq1$J00_J99)$stats, labels = boxplot.stats(arq1$J00_J99)$stats, y = 0.5)
    text(x = boxplot.stats(arq1$V01_Y89)$stats, labels = boxplot.stats(arq1$V01_Y89)$stats, y = 2.5)
    

    这显示了标签在上部箱线图中的一个重叠。

    为避免这种情况,请执行两次text,将不同的统计参数选择到不同的 y 高度:

    text(x = boxplot.stats(arq1$V01_Y89)$stats[2:5], labels = boxplot.stats(arq1$V01_Y89)$stats[2:5], y = 2.5)
    text(x = boxplot.stats(arq1$V01_Y89)$stats[1], labels = boxplot.stats(arq1$V01_Y89)$stats[1], y = 2.)
    

    上面我要求包含从 2 到 5 的参数:第一个四分位数、中位数、第三个四分位数和 y=2.5 处的最大值和 y=2 处的最小值

    这解决了任何类型的统计参数重叠到箱线图

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多