【问题标题】:How to plot group bar plot with linear trendline in R如何在 R 中使用线性趋势线绘制组条形图
【发布时间】:2022-07-05 22:30:35
【问题描述】:

此条形图使用 MS excel 创建。现在,我想在 R 中使用 Plotly 或 ggplot2 制作这样的条形图

这是我的数据,

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

提前致谢。

【问题讨论】:

  • 您的地块有计划支出和实际支出,但您的数据没有。

标签: r ggplot2 plotly


【解决方案1】:

你可以试试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多