0

First, I did try to follow the link below (along with a bunch of other google efforts...):

Grouped barplot in R with error bars

My dataset is quite small (and R still greatly confuses me being new to any sort of programming). My spatial data looks something like this with fake data:

Site1 <- c(1, 2, 3, NA, 3, 4, 8, 1, 4)
Site2 <- c(2, 4, 8, 8, 2, NA, 1, 2, 5)
Site3 <- c(5, NA, 4, NA, 8, 1, 3, 4 ,6)
my.data <- cbind(Site1, Site2, Site3)
my.data <- cbind(Site1, Site2, Site3)
my.data <- data.frame(a = c("during", "during", "during", "during",     "after", "after", "after", "after", "after"), my.data)
colnames(my.data) <- c("time", "GA", "GI", "DI")

I want to create a grouped barplot of the means of each site with error bars representing 1 standard deviation, where "during" and "after" are grouped by site (GA, GI, DI). I tried to do this by creating a data frame of means and plotting those. Which works great. Except I can't figure out how to add the error bars. I would appreciate any help. I know this is an elementary question but I am in baby steps when it comes to coding. So yeah.

Community
  • 1
  • 1
user3281487
  • 97
  • 1
  • 1
  • 9
  • So where exactly did you get stuck following the other answer? I don't see any reason not to close this as a duplicate. Show your code, describe exactly where you are having a problem. We're not here to code for you, we are here to answer specific programming questions. – MrFlick Sep 06 '16 at 02:53

1 Answers1

1

This should solve it. Please note that I plotted the geom_errorbar using the code from the link you provided. And it seems to work fairly well!

Your problem was that your data was not shaped properly. Go through this link once

The code -

Site1 <- c(1, 2, 3, NA, 3, 4, 8, 1, 4)
Site2 <- c(2, 4, 8, 8, 2, NA, 1, 2, 5)
Site3 <- c(5, NA, 4, NA, 8, 1, 3, 4 ,6)
my.data <- cbind(Site1, Site2, Site3)
my.data <- cbind(Site1, Site2, Site3)
my.data <- data.frame(a = c("during", "during", "during", "during","after", "after", "after", "after", "after"), my.data)
colnames(my.data) <- c("time", "GA", "GI", "DI")

library(reshape2)
library(dplyr)
data <- reshape(my.data,direction="long", varying=names(my.data)[-1],
                timevar="Site", times=names(my.data)[-1], v.names="value")
df <- data %>% 
  group_by(Site,time) %>%
  summarize(Mean=mean(value,na.rm=T),SD=sd(value,na.rm=T))

library(ggplot2)
ggplot(df, aes(x=Site,y=Mean,fill=time)) +
  geom_bar(position="dodge",stat="identity",color="black") +
  geom_errorbar(aes(ymin=Mean-SD,ymax=Mean+SD),width=0.2,position=position_dodge(0.9))

Output :

enter image description here

prateek1592
  • 547
  • 5
  • 13
  • Great, thank you so much for your help! i couldn't figure out what my problem was... reshaping the data was definitely the key to my issue. Thanks again! – user3281487 Sep 07 '16 at 16:20
  • the above code worked great for most of my data. However, when I use it for my full data set I get the following error after calling ggplot _Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error in data.frame(x = c("DI", "DI", "GA", : arguments imply differing number of rows: 12, 0_ . I wonder if it is because some of my sites have no data at all? As in the "during" bar is a zero (or actually an NA). – user3281487 Sep 08 '16 at 18:49
  • So I tested the code with fake data by adding NAs to a couple of sites, and your code still works. I just get a warning that some bars were not plotted (due to missing data). This is exactly correct, and with the fake dataset the code works. Just not with my real data... I checked the format/shaping of the dataset and it looks exactly like the fake data, except I have a few more sites in the real data. I'm so confused! – user3281487 Sep 08 '16 at 19:07
  • Ok I checked the code and I had a typo... so it works now! – user3281487 Sep 08 '16 at 20:44