0

I am struggling to find out the mistake I am making in trying to produce this barplot with standard deviation error bars. The result I get is of error bars not properly positioned (vertically, in my opinion) in my bars.

My dataset is

 'data.Nema':   11 obs. of  6 variables:

 $ STATION: Factor w/ 3 levels "FARO","ISLA D",..: 1 1 1 1 2 2 2 2 3 3 ...

 $ SEASON : Factor w/ 4 levels "Summer15","Winter15",..: 1 3 2 4 1 3 2 4 1  

 $ N      : int  5 3 5 3 5 3 5 3 5 3 ...

 $ mean   : num  2285 4419 795 1057 3694 ...

 $ sd     : num  511 1425 216 597 789 ...

 $ se     : num  256 1008 108 422 395 ...

my code is:

p4 <- ggplot(data=data.Nema, aes(x=STATION, y=mean,       fill="Nematoda")) +
     geom_bar(stat = "identity",position=position_dodge()) +
     facet_wrap(facets = ~data.Nema$SEASON, ncol =3)+
      theme_bw()+
      theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
    panel.background = element_blank(), axis.line = element_line(colour = "black")) +
              theme(strip.text.x = element_text(size = 8, colour = "black"))+
      theme(axis.text=element_text(size = 8), axis.text.x=element_text(angle = 90, hjust = 1), axis.title.x = element_text(size = 8, vjust = 0.1), 
    axis.title.y = element_text(size = 9, vjust = 0.3), legend.position="bottom",legend.text=element_text(size=8), legend.title = element_text(size = 8)) + ylab(expression(paste("Individual numbers", "  10",cm^-2 ))) +
      xlab("" ) + 
      geom_errorbar(aes(ymin = data.Nema$mean - sd, 
                ymax = data.Nema$mean + sd, 
                width = 0.1), position=position_dodge(0.9)) +
        facet_wrap(facets = ~data.Nema$SEASON, ncol =3)
      plot(p4)

Thank you for your help

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 08 '18 at 03:05
  • structure(list(STATION = structure(c(1L, 1L, 1L, 1L), .Label = c("FARO", "ISLA D", "CREEK"), class = "factor"), SEASON = structure(c(1L, 3L, 2L, 4L), .Label = c("Summer15", "Winter15", "Spring15", "Spring16" ), class = "factor"), N = c(5L, 3L, 5L, 3L), mean = c(2284.93355351105, 4419.28127703075, 795.2, 1056.66666666667), sd = c(511.394995921795, 1425.24532727231, 216.172847508654, 596.913170681744), se = c(255.697497960898, 1007.80063576869, 108.086423754327, 422.081350768624)), .Names = c("STATION", "SEASON", "N", "mean", "sd", "se"), row.names = c(NA, 4L), class = "data.frame") – Francesca Pasotti Nov 08 '18 at 10:41
  • Is this what you asked? Sorry, I was not aware of this requirement. Thank you for willing to help! – Francesca Pasotti Nov 08 '18 at 10:41
  • Yes, that's exactly the kind of info you need. You can use the edit button to add the data to your question. – aosmith Nov 08 '18 at 16:05
  • I think your problem is with using dollar sign notation along with facets. See [here](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) for a full explanation. I believe this won't be a problem in future (current?) versions of ggplot2, but you still should refer directly to the variable names when using ggplot2. – aosmith Nov 08 '18 at 16:06
  • Dear aosmith, you were so right! Thank you! I removed all the $ from the code and it worked just fine. I am sorry it was such a trivial detail, but I never bumped into the very useful link you mentioned. thank you so much! you took my headache away and I can finally go on living! :) – Francesca Pasotti Nov 09 '18 at 07:17

0 Answers0