user1997567
user1997567

Reputation: 439

Plot grouped data using ggplot

Hi I have a table like this:

Question Age_Group Value
Question 1 10-20 15
Question 2 10-20 16
Question 3 10-20 12
.....
Question 1 20-30 13
Question 2 20-30 15
Question 3 20-30 18

I want to build a histogram where questions are in y - axis, age groups in x - axis and by bars the comparision is visible based on Value column.

How to make this possible? Regards

Upvotes: 0

Views: 110

Answers (1)

user12728748
user12728748

Reputation: 8506

Do you want facets? E.g. something like this:

set.seed(123)
DF <- data.frame(Question = rep(paste0("Question ", 1:10), 80),
  Age_Group = factor(sample(c("10-20", "20-30", "30-40", "40-50"), 
    800, replace=TRUE)),
  Value = sample(8:30, 800, replace = TRUE))
DF$Question <- factor(DF$Question, unique(DF$Question))

library(ggplot2)  
ggplot(DF, aes(x=Value)) + 
  geom_histogram() +
  facet_grid(Question ~ Age_Group) +
  theme(strip.text.y = element_text(angle = 0))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2020-08-05 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions