0

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?

Michiel
  • 17
  • 4

1 Answers1

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

Gnueghoidune
  • 1,237
  • 4
  • 13
  • It works perfect for my simplified dataset. When i try to use it for the larger dataset, I first of all cannot use the tibble functio "error:object not found" and secondly when i use ggplot i get the correct values but there appear no bars. Any idea what the problem might be? – Michiel Apr 14 '22 at 08:06
  • can you show the beginning of your data? dput(head(yourdataframe)) – Gnueghoidune Apr 14 '22 at 08:47
  • 1
    I managed to fix it the width was just so narrow I couldn't see them without zooming in... – Michiel Apr 14 '22 at 09:01