条形图有几个条形图

barplot with several bars

我有一个包含多个列的大型数据集。现在我想制作一个条形图来可视化结果,我将首先制作一个看起来像我的数据集

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)

现在我想制作一个条形图,x 轴是 4 个案例,Y 轴是平均身高,M 和 F 的两个不同的条表示平均身高。 它应该是这样的: 除了使用 barplot(),我真的不知道如何开始或做什么,有人可以帮忙吗?

您可以这样做:将您的矢量放入小标题中,以便您可以轻松地将它们传递给您的 ggplot() 调用。

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"))