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

Merging two boxplots into one using ggplot2

我找到了一种使用基本 R 箱线图合并两个箱线图的方法。但是,我无法产生紧张情绪。我在网上找到了一种方法,但似乎没有用。有没有办法在 ggplot2 中做到这一点?

我想将图片左侧的两个箱线图合并为一个,如右侧,但抖动颜色不同。

感谢您的帮助。

这是我的代码:

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 页面,这里是箱线图页面的 link:https://plotly.com/r/box-plots/