具有多个条形的条形图

发布于 2025-01-21 04:05:41 字数 551 浏览 1 评论 0原文

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

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() 之外,我真的不知道如何开始或做什么,任何人都可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2025-01-28 04:05:41

您可以这样做:将矢量放入 tibble 中,以便您可以轻松地将它们传递给您的 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"))

输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文