如何避免R中箱线图的标签/文本重叠?

如何避免R中箱线图的标签/文本重叠?,r,ggplot2,boxplot,violin-plot,ggrepel,R,Ggplot2,Boxplot,Violin Plot,Ggrepel,我正在使用ggplot2绘制箱线图和小提琴图,以查看数据的分布。方框图的四分位数彼此非常接近。这就是它导致重叠的原因 我使用了ggrepel::geom_label_repel,但它不起作用。如果删除geom\u label\u repel,则某些标签会重叠 这是我的R代码和示例数据: dataset <- data.frame(Age = sample(1:20, 100, replace = T)) ggplot(dataset, aes(x = "", y = Age)) +

我正在使用
ggplot2
绘制箱线图和小提琴图,以查看数据的分布。方框图的四分位数彼此非常接近。这就是它导致重叠的原因

我使用了
ggrepel::geom_label_repel
,但它不起作用。如果删除
geom\u label\u 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)

dataset这里有一个稍微不同的方法,没有
ggrepel
。半小提琴图实际上是一个经典的密度图,只是垂直的。这是情节的基础。我正在使用
ggstance::geom\u boxploth
添加一个水平长方体图。对于标签,我们不能再使用
stat\u summary
,因为我们不能对x个值进行总结(也许有人知道怎么做,我不知道)。所以我习惯于一次性预先计算分位数。您可以将箱线图的位置设置为0,然后只填充密度图,以避免在计算线段等时遇到一些实际问题

然后,您可以根据自己的喜好对图形进行非常巧妙的微调

库(tidyverse)
图书馆(GGSTANTE)
#> 
#>附加程序包:“GGSTANTE”
#>以下对象被“package:ggplot2”屏蔽:
#> 
#>geom_errorbarh,GeomErrorbarh
数据集%
不耐烦

my_y这里有一个稍微不同的方法,没有
ggrepel
。半小提琴图实际上是一个经典的密度图,只是垂直的。这是情节的基础。我正在使用
ggstance::geom\u boxploth
添加一个水平长方体图。对于标签,我们不能再使用
stat\u summary
,因为我们不能对x个值进行总结(也许有人知道怎么做,我不知道)。所以我习惯于一次性预先计算分位数。您可以将箱线图的位置设置为0,然后只填充密度图,以避免在计算线段等时遇到一些实际问题

然后,您可以根据自己的喜好对图形进行非常巧妙的微调

库(tidyverse)
图书馆(GGSTANTE)
#> 
#>附加程序包:“GGSTANTE”
#>以下对象被“package:ggplot2”屏蔽:
#> 
#>geom_errorbarh,GeomErrorbarh
数据集%
不耐烦

my_y使用标准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.)
 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箱线图命令时,使用命令
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.)
 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个统计参数包括到图表中。
例如:

这显示了标签在上部箱线图中的一个重叠部分 要避免这种情况,请执行两次
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.)
 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
箱线图
命令时,使用命令
text
将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.)
 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时的最小值

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

library(ggrepel)
,然后您只需执行统计摘要(geom=“label\u repel”,…)
谢谢。成功了。你对问题的第二部分(半盒图和半小提琴图)有什么想法吗?
library(ggrepel)
,然后你只需做统计摘要(geom=“label\u repel”,…)
谢谢。成功了。你知道问题的第二部分(半盒图和半小提琴图)吗?谢谢你的回答。它工作得很好。我试图固定密度图的宽度(这可能不符合逻辑,但我只是尝试过),以获得一致大小的长方体图和密度图;但是,
width
选项不适用于
geom\u density()
。由于它不起作用,我从
ggstance::geom_boxploth()
中删除了
width=0.05
。它也不起作用,因为一些变量集中在一些值周围,并且无法看到某些变量的方框图。你知道如何把两个图的宽度按一个好的比例排列,这样我就能得到一个好的图吗?很高兴我能帮上忙。我不确定我是否完全理解你所说的“大小一致的b图和d图”是什么意思……因为这个样本是正态分布的,所以看起来很正常。然而,在实际数据中,一些值支配着v