我正在尝试使用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
我想生成两种不同类型的箱线图,如这些示例箱线图所示。任何人都可以帮助生成这些箱形图。谢谢
在您的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
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))