How to correct labels for boxplot get the p-values at each pair in in R

Viewed 639

I have a sample of the data as follows:

df <- tribble(
    ~capacity1, ~capacity2, ~capacity3, ~capacity4, ~capacity5, ~capacity6, ~capacity7, ~capapcity8,
    75, 88, 85, 71, 98, 76, 71, 57,
    80, 51, 84, 72, 59, 81, 70, 64,
    54, 65, 90, 66, 93, 88, 77, 59,
    59, 87, 94, 75, 74, 53, 56, 87,
    52, 55, 64, 77, 50, 64, 83, 87,
    33,22,66,67,99,87,40,90,)

I want to get the following graph.

enter image description here

As you can see, capacity 1 goes with capacity2 and produce one label as Capacity1. capacity 3 with capacity4= Capapcity2, capacity5 and capacity6=Capaccity3 and capacity 7 with capacity8= Capacity4. Next, I would like to get p-values. That would be good if we could order each pair of boxes ( e.g., capacity1 with capacity2= Capacity1).

2 Answers

If we need pairwise plots, we can split into a list of datasets for each pair of columns, then use ggboxplot from ggpubr

library(dplyr)
library(tidyr)
library(purrr)
library(patchwork)
library(rstatix)
library(ggpubr)
lst1 <- df %>% 
        # // split every 2 columns
        split.default(as.integer(gl(ncol(.), 2, ncol(.)))) %>% 
        # // loop over the list
        map(~ {
          # // reshape to long format
           dat <- pivot_longer(.x, everything())
           # // get the t.test p value
           stat_test <- dat %>%
                         t_test(value ~ name)%>% 
                         adjust_pvalue(method = "bonferroni") %>%      
                         add_significance("p.adj") %>% 
                         add_xy_position(x = "name")
        # // create the boxplot
        ggboxplot(dat, x = 'name', y = 'value')+
                   stat_pvalue_manual(stat_test, 
                        label = "p.adj", tip.length = 0.01)
        })       

Now, we wrap the list of plots with wrap_plots from patchwork

wrap_plots(lst1)

-output

enter image description here

Try this approach. Your data is in wide format. First you have to transform to long using pivot_longer(). After that you can use ggplot2 to sketch the plot with geom_boxplot(). In order to add p-values you need to define the proper test and using stat_compare_means(). Here the code using t.test:

library(ggplot2)
library(dplyr)
library(tidyr)
#Code
df %>% pivot_longer(everything()) %>%
  ggplot(aes(x=name,y=value,fill=name,group=name))+
  geom_boxplot()+
  stat_compare_means(label = "p", method = "t.test",
                     ref.group = ".all.")+
  labs(fill='Variable')

Output:

enter image description here

For grouping:

#Data for groups
groups <- data.frame(name=c("capacity1", "capacity2", "capacity3", "capacity4", "capacity5", 
                            "capacity6", "capacity7", "capapcity8"),
                     group=paste0('Group.',c(1,1,2,2,3,3,4,4)),stringsAsFactors = F)
    #Code
    df %>% pivot_longer(everything()) %>%
      left_join(groups) %>%
      ggplot(aes(x=name,y=value,fill=name,group=name))+
      geom_boxplot()+
      facet_wrap(.~group,scales = 'free',nrow = 1,strip.position = 'bottom')+
      labs(fill='Variable')+
      theme(strip.placement = 'outside',strip.background = element_blank(),
            legend.position = 'none')

Output:

enter image description here

Related