如何绘制具有多个变量的图?
Posted
技术标签:
【中文标题】如何绘制具有多个变量的图?【英文标题】:How to do a plot with multiple variables? 【发布时间】:2021-08-31 10:53:31 【问题描述】:我想制作一个条形图,其中 x 轴上的一个值对应于几年的结果,并且每年的结果按月细分。现在我已经设法用这些年来创造了一个情节。它的代码是这样的:
ggplot(data = df, aes(x = Hours, y = Result, fill = factor(Year))) +
geom_bar(position = "dodge", stat = "identity", colour = "black") +
geom_text(aes(label = Result,
vjust=ifelse(Result < 65, -0.3, 1.1)),
position=position_dodge(width=0.9),
size = 2.5) +
theme_minimal()
我试过这个:
ggplot(data = df, aes(x = Hours, y = Result, fill = factor(Year), position_fill = factor(Month)))+
geom_bar(position = "dodge", stat = "identity", colour = "black") +
geom_text(aes(label = Result,
vjust=ifelse(Result < 65, -0.3, 1.1)),
position=position_dodge(width=0.9),
size = 2.5) +
theme_minimal()
但是,它只会制造更多的酒吧,但我希望一年的酒吧会充满几个月。
示例数据框(有点短):
structure(list(Hours = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
), Year = c(2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021,
2021, 2021, 2021, 2021, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2021, 2021, 2021, 2021, 2021, 2021), Month = c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,
5, 6), Result = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 4, 1, 0, 7, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 1, 1, 0, 2, 0, 1, 1, 0, 3, 1, 2, 0, 6, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 3, 3, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1,
0, 2, 2, 1, 8, 5, 1, 7, 1, 3, 5, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 5, 0, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 1, 0, 0, 3, 1, 2,
0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 1, 1, 0, 2, 2, 1, 7, 5, 0, 7, 1, 2, 4, 0)), row.names = c(NA,
-180L), class = c("tbl_df", "tbl", "data.frame"))
提前感谢您对我的帮助。
【问题讨论】:
请分享一小部分可重复的数据样本。dput(df[1:10, ])
将给出df
的前 10 行的复制/粘贴版本,包括所有类和结构信息。选择一个足以说明问题的子集。
我添加了 2 个不同小时的数据框。我觉得应该够了。
【参考方案1】:
我认为你想要的是一个 stacked 条形图,你可以使用 barplot 命令来实现这一点,请参阅https://www.geeksforgeeks.org/stacked-bar-chart-in-r/
但是,您很可能必须将数据转换为具有适当列的矩阵。我建议您使用 RStudio 控制台中的 ?barplot 命令查看完整文档。
【讨论】:
以上是关于如何绘制具有多个变量的图?的主要内容,如果未能解决你的问题,请参考以下文章