Error bars incorrectly positioned in a stacked bar graph (r)









up vote
3
down vote

favorite












I was trying to make a stacked bar graph with error bars based on this, previously answered question Making stacked bar plot with specified error bar values in R



However, my error bars are incorrectly positioned. I have tried changing the order of SD, and that shifts the error bars relative to each other, but they still do not line up with the stacked bars.



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


enter image description here



Also, I'm very new to R, and even newer to stack overflow (this is my first question!)- so any advice on how to format questions more clearly or more efficiently search the site for answers, is always appreciated :)










share|improve this question



























    up vote
    3
    down vote

    favorite












    I was trying to make a stacked bar graph with error bars based on this, previously answered question Making stacked bar plot with specified error bar values in R



    However, my error bars are incorrectly positioned. I have tried changing the order of SD, and that shifts the error bars relative to each other, but they still do not line up with the stacked bars.



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


    enter image description here



    Also, I'm very new to R, and even newer to stack overflow (this is my first question!)- so any advice on how to format questions more clearly or more efficiently search the site for answers, is always appreciated :)










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I was trying to make a stacked bar graph with error bars based on this, previously answered question Making stacked bar plot with specified error bar values in R



      However, my error bars are incorrectly positioned. I have tried changing the order of SD, and that shifts the error bars relative to each other, but they still do not line up with the stacked bars.



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


      enter image description here



      Also, I'm very new to R, and even newer to stack overflow (this is my first question!)- so any advice on how to format questions more clearly or more efficiently search the site for answers, is always appreciated :)










      share|improve this question















      I was trying to make a stacked bar graph with error bars based on this, previously answered question Making stacked bar plot with specified error bar values in R



      However, my error bars are incorrectly positioned. I have tried changing the order of SD, and that shifts the error bars relative to each other, but they still do not line up with the stacked bars.



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


      enter image description here



      Also, I'm very new to R, and even newer to stack overflow (this is my first question!)- so any advice on how to format questions more clearly or more efficiently search the site for answers, is always appreciated :)







      r ggplot2 errorbar stackedbarseries






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 18:14









      Tjebo

      2,1171126




      2,1171126










      asked Nov 10 at 17:52









      Miranda

      184




      184






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think this happens because the variable is plotted top to bottom on the y-axis, but sum in cumsum(value) is calculated assuming they are plotted bottom to top.



          I reordered the data frame before taking the sum then it works:





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




          Created on 2018-11-10 by the reprex package (v0.2.1)






          share|improve this answer
















          • 1




            Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
            – Miranda
            Nov 10 at 23:35










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241804%2ferror-bars-incorrectly-positioned-in-a-stacked-bar-graph-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I think this happens because the variable is plotted top to bottom on the y-axis, but sum in cumsum(value) is calculated assuming they are plotted bottom to top.



          I reordered the data frame before taking the sum then it works:





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




          Created on 2018-11-10 by the reprex package (v0.2.1)






          share|improve this answer
















          • 1




            Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
            – Miranda
            Nov 10 at 23:35














          up vote
          1
          down vote



          accepted










          I think this happens because the variable is plotted top to bottom on the y-axis, but sum in cumsum(value) is calculated assuming they are plotted bottom to top.



          I reordered the data frame before taking the sum then it works:





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




          Created on 2018-11-10 by the reprex package (v0.2.1)






          share|improve this answer
















          • 1




            Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
            – Miranda
            Nov 10 at 23:35












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I think this happens because the variable is plotted top to bottom on the y-axis, but sum in cumsum(value) is calculated assuming they are plotted bottom to top.



          I reordered the data frame before taking the sum then it works:





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




          Created on 2018-11-10 by the reprex package (v0.2.1)






          share|improve this answer












          I think this happens because the variable is plotted top to bottom on the y-axis, but sum in cumsum(value) is calculated assuming they are plotted bottom to top.



          I reordered the data frame before taking the sum then it works:





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




          Created on 2018-11-10 by the reprex package (v0.2.1)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 23:21









          seisdrum

          1765




          1765







          • 1




            Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
            – Miranda
            Nov 10 at 23:35












          • 1




            Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
            – Miranda
            Nov 10 at 23:35







          1




          1




          Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
          – Miranda
          Nov 10 at 23:35




          Thanks so much! I had tried different ways of reordering the stacked bars (e.g [order(df$fill_var,decreasing=T),]) but that hadn't fixed the problem. This works perfectly!
          – Miranda
          Nov 10 at 23:35

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241804%2ferror-bars-incorrectly-positioned-in-a-stacked-bar-graph-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto