Search code examples
rggplot2boxplot

How to create boxplot graph and same coloured spots around them


I have a question about this kind of graph.

I'm interested on how to obtain/show the spots around the same coloured box.

Thanks in advance

enter image description here


Solution

  • Update on OP request: enter image description here

    The code:

    library(ggpubr)
    
    
    # create fake data mtcars1
    mtcars1 <- mtcars %>% 
      add_row(mtcars[1,]) %>% 
      mutate(group = rep(c("A", "B", "C"), 11))
    
    compare_means(disp ~ cyl, data = mtcars1,  method = "t.test")
    my_comparisons <- list( c("4", "6"), c("6", "8"), c("4", "8") )
    
    ggboxplot(mtcars1, x = "cyl", y = "disp", fill = "cyl", add = "jitter") +
      scale_fill_manual(values=c("#0433ff", "#fe403f", "#66cd00")) +
      stat_compare_means(comparisons = my_comparisons)+
      stat_compare_means(label.y = 700) +
      facet_grid(.~group, switch = "x")+
      xlab(NULL) +
      theme(strip.background = element_blank(),
            strip.placement = "outside")
    

    Here is an example using the ggpubr package and the mtcars dataset:

    library(ggpubr)
    
    compare_means(disp ~ cyl, data = mtcars,  method = "t.test")
    my_comparisons <- list( c("4", "6"), c("6", "8"), c("4", "8") )
    
    ggboxplot(mtcars, x = "cyl", y = "disp", color = "cyl", add = "jitter") +
      stat_compare_means(comparisons = my_comparisons)+
      stat_compare_means(label.y = 700) 
    

    enter image description here