【问题标题】:Error bars incorrectly positioned in a stacked bar graph (r)错误条在堆积条形图中的位置不正确 (r)
【发布时间】:2018-11-10 17:52:18
【问题描述】:

我正在尝试根据之前回答的问题 Making stacked bar plot with specified error bar values in R 制作带有误差线的堆叠条形图

但是,我的误差线位置不正确。我尝试更改 SD 的顺序,这会相对于彼此移动误差线,但它们仍然没有与堆叠的条对齐。

x <- data.frame(Period = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"), Sample = c("Day","Day","Day","Day","Day","Day","Day"), "12AM-6AM" = c(23.14,16.43,16,22.71,36.86,87.14,110.71), "6AM-12PM" = c(16.14,20.86,18.43,16.71,15.14,14.29,16), "12PM-6PM" = c(26.86,23.71,25.57,23.43,35.29,38,30), "6PM-12AM" =c(35.86,34.14,31.71,36.43,45.57,44,27.14))

library(dplyr)
library(reshape2)

 mx <- melt(x,  id.vars=1:2)
 mx <- mx %>% group_by(Period) %>%
      mutate(pos = cumsum(value)) %>%
      ungroup() %>%
      mutate(sd = c( 5.4, 2.7, 4.7, 4.4, 8.2, 13.2, 20.7,
                    5.6, 2.3, 5.4, 5.5, 1.6, 4.1, 3.1,
                   5.2, 5.6, 5.9, 3.5, 6.3, 4.5, 6.9,
                   3.5, 6.0, 5.9, 6.2, 8.2, 9.0, 2.4
                   ),
             upper = pos + sd/2,
             lower = pos - sd/2)

    days<-c("Mon", "Tue", "Wed","Thu","Fri", "Sat", "Sun")
    ggplot(mx, aes(x=Period, y=value, fill=variable), xLabels=NA) +
      geom_bar(stat="identity") +
      facet_grid(~Sample) + geom_errorbar(aes(ymin = lower, ymax = upper),
 width = .2, col = "red") +
      theme_bw() + scale_x_discrete(limits = days) +
      xlab(" Day of the Week") + scale_fill_grey() +
      ylab ("Number of calls")

另外,我对 R 非常陌生,甚至对堆栈溢出也很陌生(这是我的第一个问题!)- 所以任何关于如何更清晰地格式化问题或更有效地在网站上搜索答案的建议,总是值得赞赏的:)

【问题讨论】:

    标签: r ggplot2 errorbar stackedbarseries


    【解决方案1】:

    我认为这是因为 variable 在 y 轴上从上到下绘制,但 cumsum(value) 中的总和是假设它们从下到上绘制的。

    我在求和之前对数据框进行了重新排序,然后它就起作用了:

    x <- data.frame(Period = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"), Sample = c("Day","Day","Day","Day","Day","Day","Day"), "12AM-6AM" = c(23.14,16.43,16,22.71,36.86,87.14,110.71), "6AM-12PM" = c(16.14,20.86,18.43,16.71,15.14,14.29,16), "12PM-6PM" = c(26.86,23.71,25.57,23.43,35.29,38,30), "6PM-12AM" =c(35.86,34.14,31.71,36.43,45.57,44,27.14))
    
    library(tidyverse)
    library(reshape2)
    #> 
    #> Attaching package: 'reshape2'
    #> The following object is masked from 'package:tidyr':
    #> 
    #>     smiths
    
    mx <- melt(x,  id.vars=1:2)
    mx <- mx %>% 
      mutate(sd = c( 5.4, 2.7, 4.7, 4.4, 8.2, 13.2, 20.7,
                     5.6, 2.3, 5.4, 5.5, 1.6, 4.1, 3.1,
                     5.2, 5.6, 5.9, 3.5, 6.3, 4.5, 6.9,
                     3.5, 6.0, 5.9, 6.2, 8.2, 9.0, 2.4)
      ) %>%
      group_by(Period) %>%
      arrange(desc(variable)) %>%
      mutate(
        pos = cumsum(value),
        upper = pos + sd/2,
        lower = pos - sd/2
      ) %>%
      ungroup()
    
    days<-c("Mon", "Tue", "Wed","Thu","Fri", "Sat", "Sun")
    ggplot(mx, aes(x=Period, y=value, fill=variable), xLabels=NA) +
      geom_bar(stat="identity") +
      facet_grid(~Sample) + geom_errorbar(aes(ymin = lower, ymax = upper),
                                          width = .2, col = "red") +
      theme_bw() + scale_x_discrete(limits = days) +
      xlab(" Day of the Week") + scale_fill_grey() +
      ylab ("Number of calls")
    

    reprex package (v0.2.1) 于 2018 年 11 月 10 日创建

    【讨论】:

    • 非常感谢!我尝试了不同的方法来重新排序堆叠的条(例如 [order(df$fill_var,decreating=T),]),但这并没有解决问题。这非常有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2013-09-12
    • 2013-12-19
    相关资源
    最近更新 更多