繁体   English   中英

使用 ggplot2 将两个箱线图合并为一个

[英]Merging two boxplots into one using ggplot2

我找到了一种使用基本 R 箱线图合并两个箱线图的方法。 但是,我不能产生抖动。 我在互联网上找到了一种方法,但似乎没有用。 有没有办法在 ggplot2 中做到这一点?

我想将图片左侧的两个箱线图合并为一个,就像右侧一样,但在不同的 colors 中存在抖动。

在此处输入图像描述

谢谢您的帮助。

这是我的代码:

boxplot(mean ~ group, data = nonsignificance.exp, xlab = "",
            ylab = "Mean of Improvement (post-test - pre-test)", main = "", col = "#E74C3C")
boxplot(mean ~ group, data = nonsignificance.con, xlab = "",
    ylab = "Mean of Improvement (post-test - pre-test)", main = "",
    add=TRUE, col="#1ABC9C")

数据:

con <- structure(list(ID = c(318, 319, 320, 321, 322, 323, 324, 325, 
                        326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 
                        339, 340), mean = c(0.3333333333, 0.6666666667, 0.3333333333, 
                                            0.6666666667, 0, 0, 0.3333333333, 0, 0, 0.6666666667, -0.3333333333, 
                                            0, -1, 0.3333333333, -0.6666666667, 0.3333333333, 0, 0.3333333333, 
                                            0.6666666667, 0.6666666667, 0, 0, 0.3333333333), group = c("Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con", "Leadership Con", "Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con", "Leadership Con", "Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con", "Leadership Con", "Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con", "Leadership Con", "Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con", "Leadership Con", "Leadership Con", 
                                                                                                       "Leadership Con", "Leadership Con")), row.names = c(NA, -23L), spec = structure(list(
                                                                                                         cols = list(ID = structure(list(), class = c("collector_double", 
                                                                                                                                                        "collector")), mean = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                          "collector")), group = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                             "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                   "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df","tbl_df", "tbl", "data.frame"))


exp <- structure(list(ID = c(101, 102, 103, 104, 105, 106, 107, 108, 
                               109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 
                               122, 123, 124, 125), mean = c(1, -1.333333333, -0.3333333333, 
                                                             1, 1, -0.6666666667, 0, 0.3333333333, 1, 1, -2, 2.666666667, 
                                                             -0.6666666667, 0, 1.333333333, 2, 1.666666667, 1.333333333, 0.3333333333, 
                                                             1.666666667, -0.3333333333, 0.6666666667, 0, 0.3333333333, 1.333333333
                               ), group = c("Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp", "Leadership Exp", "Leadership Exp", 
                                            "Leadership Exp", "Leadership Exp")), row.names = c(NA, -25L), spec = structure(list(
                                              cols = list(ID = structure(list(), class = c("collector_double", 
                                                                                             "collector")), mean = structure(list(), class = c("collector_double", 
                                                                                                                                               "collector")), group = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                  "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                        "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

通过使用 Plotly 您可以解决您的问题,我认为,添加抖动比基本箱线图更容易。 据我所知,如果你想让它们像你的照片一样,名字应该是相同的。

fig = plot_ly(type = "box")
fig = fig %>% add_boxplot(y = con$mean, name = "group1", jitter = 0.3) 
fig %>% add_boxplot(y = exp$mean, name = "group1", jitter = 0.3)

在此处输入图像描述

如果您想指定更多图表,请查看 plotly R 页面,这里是箱线图页面的链接: https://plotly.com/r/box-plots

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM