如何避免 R 中箱线图 labels/texts 的重叠?

How to avoid overlapping of labels/texts of boxplot in R?

我正在绘制箱线图和小提琴图,以查看使用 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)

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

这里的方法略有不同,没有 ggrepel。半个小提琴图实际上是一个经典的密度图,只是垂直的。这是情节的基础。我正在添加带有 ggstance::geom_boxploth 的水平箱线图。对于标签,我们不能再使用 stat_summary,因为我们不能对 x 值进行汇总(也许有人知道该怎么做,我不知道)。所以我用一次性预算了分位数。您可以将箱线图的位置设置为 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 创建

使用标准的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 处的最小值

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

当使用标准 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 处的最小值。
这解决了重叠到箱线图中的任何类型的统计参数