我找到了一種使用基本 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"))
uj5u.com熱心網友回復:
我認為通過使用 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 頁面,這里是 boxplot 頁面的鏈接:https ://plotly.com/r/box-plots/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/360788.html
上一篇:如何在ggplot中保留所有圖例
下一篇:拼湊不會為組合圖分配共同的圖例
