-1

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

Bloxx
  • 1,495
  • 1
  • 9
  • 21
  • Please provide your code and data, so we can easily reproduce your question. – Bloxx Nov 18 '21 at 13:15
  • 1
    Thanks @Bloxx, I have provided the code and data, but I only have boxplot code, not ggplot2. – Faisal Mustafa Nov 18 '21 at 13:23
  • ok. You should find your answer here: [link](https://stackoverflow.com/questions/14604439/plot-multiple-boxplot-in-one-graph) – Bloxx Nov 18 '21 at 13:29
  • 1
    And for future questions. Use `dput()` function to provide data. In your editor, run function `dput(nonsignificance.exp)` and copy the output as a code in your question. The same for your other dataset. – Bloxx Nov 18 '21 at 13:31
  • Actually no @Bloxx. I want to combine both boxplots into one as in the second picture. In the link you provided, the boxplots are combined side by side, but I need to do it one over another. Thanks though – Faisal Mustafa Nov 18 '21 at 13:34

1 Answers1

1

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/

patula
  • 136
  • 5
  • 1
    This is great. I only need to add some additional codes to show all data points to glitter. ```boxpoints = "all", pointpos = 0``` – Faisal Mustafa Nov 18 '21 at 15:54
  • @FaisalMustafa good job! happy to see it's working. if you want to delete legends because they have same name, you can use %>% layout(showlegend = FALSE) – patula Nov 18 '21 at 16:02