2

I have created a bar chart displaying proportion of time spent on different behaviours for groups of lemurs. However I am placed with two problems.

1) I had hoped to use standard error bars in place of my standard deviation bars. I am unsure in how to incorporate it into my existing code. My current ggplot output is as follows:

 data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE),)
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

df4 <- data_summary(mydata_bc, varname="Time", 
                    groupnames=c("Group", "Behaviour"))

p <- ggplot(df4, aes(x=Behaviour, y=Time, fill=Group)) + 
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=Time-sd, ymax=Time+sd), width=.2,
                position=position_dodge(0.9))

2) I also had hoped to change the order of my behaviours on the x axis.

Any help would be greatly appreciated.

Current bar chart Current bar chart

My csv data: https://drive.google.com/file/d/1UWJoluv3MWwXoQg2zcDORDJiWuIA8j4f/view?usp=sharing

1 Answers1

0

I replaced:

sd = sd(x[[col]], na.rm=TRUE)

With:

se = sd(x[[col]], na.rm=TRUE) / sqrt(sum(!is.na(x[[col]])))

Which is the SD divided by the square root of the length.

There was also an extra comma in your data_summary function.

You can change the order of columns by reordering the factor.

mydata_bc$Behaviour <- factor(mydata_bc$Behaviour, levels = c("Resting","Feeding","Socialising","Locomotion"))

Then you can plot.

 data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      se = sd(x[[col]], na.rm=TRUE) / sqrt(sum(!is.na(x[[col]]))))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

mydata_bc$Behaviour <- factor(mydata_bc$Behaviour, levels = c("Resting","Feeding","Socialising","Locomotion"))

df4 <- data_summary(mydata_bc, varname="Time", 
                    groupnames=c("Group", "Behaviour"))

p <- ggplot(df4, aes(x=Behaviour, y=Time, fill=Group)) + 
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=Time-se, ymax=Time+se), width=.2,
                position=position_dodge(0.9))

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • Hi Ian, thanks for your response. I used your code and sadly it wouldn't work, I received the error message as follows: `Error in c(mean = mean(x[[col]], na.rm = TRUE), se = sd(x[[col]], na.rm = TRUE)/sqrt(sum(!is.na(x[[col]]))), : argument 3 is empty` – Ben Morrison Apr 03 '20 at 10:09
  • Also, I've now included my csv data sheet if that is of any help to you. – Ben Morrison Apr 03 '20 at 10:18
  • Hi @BenMorrison, I found a stray comma in your original code. I also edited my post to reorder the columns. – Ian Campbell Apr 03 '20 at 13:31
  • Hi Ian, thanks it worked! you've been incredibly helpful – Ben Morrison Apr 03 '20 at 18:02