Home > OS >  Merging two boxplots into one using ggplot2
Merging two boxplots into one using ggplot2

Time:11-19

I found a way to merge two boxplots using base R boxplot. However, I could not produce jitters. There is a way I found on the internet but it didn't seem to work. Is there a way to do it in ggplot2?

I would like to combine two boxplots as in the left of the picture to be one, as in the right, but with jitters in different colors.

enter image description here

Thank you for your help.

This is my code:

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")

Data:

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"))

CodePudding user response:

By using Plotly you can solve your problem I think, adding jitter is easier than base boxplots. to my knowledge, names should be equal if you want to put them like your picture.

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)

enter image description here

if you want to specify more your graph, check out plotly R page, here is a link to the boxplot page: https://plotly.com/r/box-plots/

  • Related