Nimrod Rimshot
Nimrod Rimshot

Reputation: 5

Missing a grouping variable when creating multiple barplots with ggplot2

I'm new to R and ggplot2 and I'm trying to wrap my head around how to create multiple barplots next to each other.

I have a dataset with 5 different variables:

All variables are in one dataframe and i have set the 3 symptom variables as factor variables.

I am able to create a bar plot for a single symptom variable color coded by gender, for example:

ggplot(myData, aes(x = DepriSymptoms, fill = Gender)) + 
  geom_bar() +
  theme_bw() +
  labs(y = "Participant Count",
       x = "Symptoms",
       title = "Depression Symptom Severity by Gender")

Example Barplot for DepriSymptoms:

Is it possible to use facet or group in ggplot2 to create 3 separate barplots like the one i made (one for Depressionsymptoms, one for FearSymptoms, one for SomaticSymptoms) next to each other?

It seems to me I am missing a variable for me to group by.

Upvotes: 0

Views: 371

Answers (1)

shirewoman2
shirewoman2

Reputation: 1928

If you want to create a facetted ggplot2 graph, you'll need to have a single variable in your data.frame by which you facet or break up the data. Let's start with some example data based on your question:

CategoryLevels <- c("none", "suspected", "light", "medium", "severe")

myData <- data.frame(SubjectID = LETTERS[1:10], 
                     Gender = sample(c("M", "F", "divers"), 10, replace = TRUE),
                     DepressionSymptoms = 
                           factor(sample(CategoryLevels, 10, replace = TRUE), 
                                  levels = CategoryLevels),
                     FearSymptoms = 
                           factor(sample(CategoryLevels, 10, replace = TRUE), 
                                  levels = CategoryLevels),
                     SomaticSymptoms =
                           factor(sample(CategoryLevels, 10, replace = TRUE), 
                                  levels = CategoryLevels))

I'm guessing your data structure looks something like this. As @Greg was saying, you'll need to convert your data from the wide format (each column is a symptom) to a longer format (one column listing the symptom and another column listing the subject's response). To do that, you'll need a subject ID to link everything so that you make sure you know whose answers were whose.

myData <- myData %>% 
      pivot_longer(names_to = "Symptom", values_to = "Response", 
                   cols = matches("Symptom"))

Now that you've got your data in a long format, you can facet on the "Symptom" column and fill the bar colors by the subjects' genders.

ggplot(myData, aes(x = Response, fill = Gender)) +
      geom_bar(stat = "count") +
      facet_wrap(~ Symptom)

bar plot

Upvotes: 0

Related Questions