'barplot with several bars [duplicate]

i have a large dataset with several columns. now i would like to make a barplot to visualise the results, I will first make a dataset that looks like mine

age <- ("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60")
gender <- ("M","F","F","F","M","M","F","M")
case <- ("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
height <- (0,200,310,0,0,175,270,150)

Now i would like to make a barplot with on the x-axis the 4 cases, on the Y-axis the average height, and two different bars for M and F indicating the average height. it should look like this: enter image description here except for using the barplot(), I don't really know how to start or what to do, can anyone help?



Solution 1:[1]

You could do like this: Put your vectors into a tibble so that you can easily pass them to your ggplot() call.

age <- c("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60")
gender <- c("M","F","F","F","M","M","F","M")
case <- c("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
height <- c(0,200,310,0,0,175,270,150)

data <- tibble(age,gender,case,height)


ggplot(data = data, aes(x = case, y = height, fill = gender)) +
  geom_col(position = position_dodge(preserve = "single"))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gnueghoidune