1

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

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

4

3 回答 3

1

这里有一个稍微不同的方法,没有ggrepel. 半个小提琴情节其实是经典的密度情节,只是垂直的。这就是剧情的基础。我正在添加一个水平箱线图ggstance::geom_boxploth。对于标签,我们不能再使用stat_summary了,因为我们不能总结 x 值(也许有人知道怎么做,我不知道)。所以我使用@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 包(v0.2.1)于 2019 年 7 月 29 日创建

于 2019-07-29T21:33:27.497 回答
0

使用标准 Rboxplot命令时,使用该命令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 的参数:第 1 个四分位数、中位数、第 3 个四分位数和最大值y=2.5和最小值y=2
这解决了任何类型的统计参数重叠到箱线图中

于 2020-08-27T12:33:09.057 回答
-1

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

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

于 2020-08-26T10:19:03.893 回答