如何在ggpubr中生成上下晶须的误差线?

问题描述 投票:0回答:2

我正在尝试使用R ggplot复制箱形图。但是,我很难添加误差线,尤其是上下水平线。同样在我的曲线图中,抖动点分布不均匀。这是我的代码和输出

survey <- data.frame(sample=rep(c("sample1","sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2"),1),
                values=c(200, 100, 150, 175, 145, 50, 75, 60, 45, 56, 300, 200, 150, 100, 125,  25, 50, 75, 45, 35))             
survey
library(ggpubr)
p1 <- ggboxplot(survey, x = "sample", y = "values", color = "black", fill = "sample", 
            palette =c("grey", "darkgrey"),
            width = 0.3, add = c("mean_se", "jitter"),
            add.params = list(size = 0.9))
p1

my_output

我想生成两种不同类型的箱线图,如这些示例箱线图所示。任何人都可以帮助生成这些箱形图。谢谢

Desired_Output_1Desired_Output_2

r ggplot2
2个回答
1
投票

在您的OP中,add(c("mean_se"))在箱形图中添加了小条,我在以下代码中将其删除。如果需要的话,请加回来。尽管ggpubr可能更灵活,但我也使用ggplot2来接近您的OP。

library(ggplot2)
library(ggpubr)
p1 <- ggboxplot(survey, x = "sample", y = "values", color = "black", fill = "sample", 
                palette =c("grey", "darkgrey"), 
                width = 0.15, add = c("jitter"),
                add.params = list(size = 0.9),
                bxp.errorbar = TRUE, bxp.errorbar.width = 0.15)
p1

enter image description here

survey$new_x = jitter(as.numeric(survey$sample), 0.5) - 0.3
p2 <- ggboxplot(survey, x = "sample", y = "values", color = "black", fill = "sample", 
                palette =c("grey", "darkgrey"), 
                width = 0.15, 
                add.params = list(size = 0.9),
                bxp.errorbar = TRUE, bxp.errorbar.width = 0.15)
p2 + geom_jitter(aes(new_x, values))

enter image description here


0
投票

这里是最终输出。感谢所有回答我的问题的人。

p1 <- ggboxplot(survey, x = "sample", y = "values", color = "black", fill = "", 
            palette =c("grey", "darkgrey"),
            width = 0.2, add = c("mean_se", "dotplot"),
            add.params = list(size = 0.6),
            bxp.errorbar = TRUE,
            bxp.errorbar.width = 0.2)
p1

Final_output

© www.soinside.com 2019 - 2024. All rights reserved.