如何在R中使用线性趋势线绘制组条图

发布于 2025-02-13 02:46:47 字数 607 浏览 2 评论 0原文

该条形图是使用MS Excel创建的。 中使用plotly或ggplot2制作这样的条形图

现在,我想在r:

这是我的数据:

data <- data.frame(Year=c('2020-2021' , '2021-2022' , '2021-2022' ,'2021-2022', '2021-2022'),
                   Quarter=c('Q4 (Jan-Mar)' ,'Q1 (April-June)' , 'Q2 (Jul-Sep)' ,'Q3 (Oct-Dec)' ,'Q4 (Jan-Mar)'),
                   Budget=c(153870.19 ,156815.8, 163501.44, 187891,246433),
                   Expenditure=c(69593.11, 104570, 98682, 104948, 130012))

我该怎么做?

This bar chart was created using MS excel. Now, I want to make a bar chart like this using Plotly or ggplot2 in R:

enter image description here

Here is my data:

data <- data.frame(Year=c('2020-2021' , '2021-2022' , '2021-2022' ,'2021-2022', '2021-2022'),
                   Quarter=c('Q4 (Jan-Mar)' ,'Q1 (April-June)' , 'Q2 (Jul-Sep)' ,'Q3 (Oct-Dec)' ,'Q4 (Jan-Mar)'),
                   Budget=c(153870.19 ,156815.8, 163501.44, 187891,246433),
                   Expenditure=c(69593.11, 104570, 98682, 104948, 130012))

How can I do this?

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

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

发布评论

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

评论(1

最笨的告白 2025-02-20 02:46:47

您可以尝试

library(tidyverse)

data %>% 
  pivot_longer(-1:-2) %>% 
  mutate(name2 = paste(name, "planned")) %>% 
  ggplot(aes(interaction(Quarter,Year, sep = "\n"), value)) + 
   geom_col(aes(fill =name), 
            position  = position_dodge2(width = 0.6), alpha = 0.8) +
   geom_smooth(method = "lm", aes(color = name2, group=name), se =  F, 
               linetype = 2,position = position_dodge2(width = 0.6)) + 
   labs(x="") + 
   theme_bw() + 
   theme(legend.title = element_blank(),
         legend.position = "top")

“在此处输入图像描述”

You can try

library(tidyverse)

data %>% 
  pivot_longer(-1:-2) %>% 
  mutate(name2 = paste(name, "planned")) %>% 
  ggplot(aes(interaction(Quarter,Year, sep = "\n"), value)) + 
   geom_col(aes(fill =name), 
            position  = position_dodge2(width = 0.6), alpha = 0.8) +
   geom_smooth(method = "lm", aes(color = name2, group=name), se =  F, 
               linetype = 2,position = position_dodge2(width = 0.6)) + 
   labs(x="") + 
   theme_bw() + 
   theme(legend.title = element_blank(),
         legend.position = "top")

enter image description here

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