Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating pairwise p values for multiple variables in R

I need help comparing 8 different drug conditions using the same assay. my data is very simple but i cannot come up with a smart way to do this. Currently it looks like this:

Drug Result
Drug1 0.5
Drug1 0.2
Drug2 0.4
Drug2 0.5
Drug3 0.4
Drug3 0.5

Etc. Each drug has about 30-40 read outs and there are 8 drugs. I want to create a box and whisker plot which I did with ggplot and include significant p values when comparing each condition against all other conditions in a pairwise fashion. Can anyone help?

Thx!

I tried to do this in ggplot using ggpubr with stat_compare_means and it was too hard to compare different results alll at once.

like image 828
Ruser123456710 Avatar asked Jan 22 '26 22:01

Ruser123456710


1 Answers

This may be a good case for the ggbetweenstats function in the ggstatsplot package, which is an extension of ggplot2. The will simultaneously perform the pairwise comparisons and plot.

Generating some sample data:

set.seed(123)
df <- data.frame(Drug = c(rep("A", 333), rep("B", 333), rep("C", 334)),
                 Result = c(runif(333), runif(333, max = 3), runif(334, max = 10)))

Plotting, with pairwise comparisons:

library(ggplot2)
library(ggstatsplot)

ggbetweenstats(data = df, x = Drug, y = Result)

Output:

enter image description here

There is a lot of customizability, including the specific pairwise test to be performed (see ?ggbetweenstats), but if you only want the box-and-whisker and remove the violin, you could do:

ggbetweenstats(data = df, x = Drug, y = Result),
                violin.args = list(width = NA, na.rm = TRUE))

Which gives:

enter image description here

like image 167
jpsmith Avatar answered Jan 24 '26 18:01

jpsmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!