Home > database >  How to create a barplot in R for multiple variables and multiple groups?
How to create a barplot in R for multiple variables and multiple groups?

Time:10-31

I want to compare the means of the variables in a barplot.

This is a portion of my dataframe.

   Group      Gender   Age   Anxiety_score   Depression_score   IUS   OBSC  
1    Anxiety    0      25      32                  29            12
2    Anxiety    1      48      34                  28            11
3    Anxiety    0      32      48                  32            12
4    Anxiety    1      24      43                  26            12
5    Anxiety    1      18      44                  26            15
6    Control    0      45      12                  11            3
7    Control    0      44      11                  11            5
8    Control    1      26      21                  10            5
9    Control    1      38      12                  NA            2
10   Control    0      18      13                  10            1

I'd like to create a barplot where each variable (Gender, Age, Anxiety_score, depression_score, IUS, ...) represents a bar and I'd like to have this for each group (anxiety vs control next to each other, not stacked) on the same graph. The height of the bar would represent the mean. For gender, I'd like to have the gender ratio. I also want to map the variables on the y axis. How do I do this in R?

CodePudding user response:

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See enter image description here


Data

df1 <-
structure(list(Group = c("Anxiety", "Anxiety", "Anxiety", "Anxiety", 
"Anxiety", "Control", "Control", "Control", "Control", "Control"
), Gender = c(0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L), Age = c(25L, 
48L, 32L, 24L, 18L, 45L, 44L, 26L, 38L, 18L), Anxiety_score = c(32L, 
34L, 48L, 43L, 44L, 12L, 11L, 21L, 12L, 13L), Depression_score = c(29L, 
28L, 32L, 26L, 26L, 11L, 11L, 10L, NA, 10L), IUS = c(12L, 11L, 
12L, 12L, 15L, 3L, 5L, 5L, 2L, 1L)), class = "data.frame", 
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))

CodePudding user response:

Are you looking for something like this?

library(tidyverse)

df %>% 
  pivot_longer(
    -Group
  ) %>% 
  group_by(Group, name) %>% 
  summarise(Mean=mean(value, na.rm=TRUE)) %>% 
  ggplot(aes(x=factor(Group), y=Mean, fill=name)) 
  geom_col(aes(group=name), position = "dodge")  
  geom_text(
    aes(label = Mean, y = Mean   0.05),
    position = position_dodge(0.9),
    vjust = 0
  )

enter image description here

  • Related