如何绘制具有多个变量的图?

时间:2021-06-15 15:01:36

标签: r ggplot2 bar-chart multiple-columns geom-bar

我想制作一个条形图,其中 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"))

预先感谢您帮助我。

1 个答案:

答案 0 :(得分:0)

我认为您想要的是堆叠条形图,您可以使用 barplot 命令实现此目的,请参阅https://www.geeksforgeeks.org/stacked-bar-chart-in-r/

但是,您很可能必须将数据转换为具有适当列的矩阵。我建议您使用 RStudio 控制台中的 ?barplot 命令查看完整文档。