Home > OS >  How can I make a bargraph with multiple comparisons between two groups in R?
How can I make a bargraph with multiple comparisons between two groups in R?

Time:06-24

I'm looking to see if I can make one bargraph with multiple comparisons between two groups.

For example, consider the following dataset. All variables are on a scale from 1 to 5 except for the categorical variable.

Extraversion <- c(4.3, 2, 4.2, 4, 2, 1.5, 3.8, 3.5, 2.1, 2.1, 3)
Anxiety <- c(1.2, 3, 1, 2.3, 5, 5, 2.3, 2.2, 5, 4.5, 3)
Depression <- c(2, 3.5, 1, 3.5, 5, 5, 1, 2, 4.3, 4.5, 1)
Sex <- c("Male", "Female", "Male", "Male", "Female", "Female", "Male", 
                "Female", "Female", "Female", "Male")

data <- data.frame(Extraversion, Anxiety, Depression, Sex)

Usually, I would make a single bargraph with the ggpubr package like the code below for the Anxiety variable.

ggbarplot(data, x = "Sex", y = "Anxiety", #data file, x variables, y variable
                       add = c("mean_se", "jitter"), #errorbars SE & jitter
                       color = "black", #Color for data points
                       fill = "Sex", #Color fill grouping variable
                       alpha = .6, #Alpha
                       palette = "jama", #ggsci package color palettes
                       position = position_dodge(.5))

That being said, my question is asking how would I combine multiple graphs like these? In the example above I would expect to have comparisons for Sex on Anxiety, Depression, and Extraversion within the same graph.

Thanks! It's my first post, so I hope that I provided all the necessary information!

CodePudding user response:

You can do this by transforming the data to long format using tidyr::pivot_longer(), then using facets.

I've adapted your ggpubr code but I get warnings regarding the SE calculation for error bars, so you should check that.

Just run data %>% pivot_longer(-Sex) to see what it does: you get two columns name for the variables and value for their values.

library(tidyr)
library(ggpubr)
library(ggsci)

data %>% 
  pivot_longer(-Sex) %>% 
  ggbarplot(x = "Sex", 
            y = "value", 
            facet.by = "name",
            add = c("mean_se", "jitter"),
            color = "black",
            fill = "Sex",
            alpha = .6,
            palette = "jama",
            position = position_dodge(.5))

Result:

enter image description here

  • Related