0

I am drawing a bar plot of means for a diversity index for a study i have done. I have calculated the index for each sample and added it to my table (which i orignally read in). I then calculated the index means for two different environments and plotted those means. However, i cannot work out how to add error bars.i understand ggplot2 is a useful tool for doing this, but cannot get my head around the explanation.

SO, basically trying to take the means of two values from a table, and plot a bargraph with error bars. this is my code at the moment

mean Shannon of river and lake

`mean_river <- mean(parasite_data$Shannon.index[1:24])
mean_lake <- mean(parasite_data$Shannon.index[25:43])`

matrix of means #

Shannon_mean <- matrix(c(mean_river, mean_lake), nrow = 1, ncol = 2, dimnames = list(c("mean"), c("River","Lake")))

plot graph #

`barplot(Shannon_mean, 

# name axis 
    xlab = "Environment", ylab = "Shannon Diversity Index", 

# title of graph 
    main = "Diversity of Parasites found on Fish from River 
            and Lake Environments", 

# size of title text and colour of bars #
    cex.main = 1, col = "gray80")`

like i said, i have attempted to use ggplot, but cannot read the data in correctly. any help would be appreciated.

2 Answers 2

1

I simulate some data that might look like yours, so you don't need to put them into separate vectors. Keep them in a dataframe

Shannon.index <- runif(43,1.5,3.5)
type = rep(c("River","Lake"),times=c(24,19)) 

df <- data.frame(Shannon.index,type)

For barplot from base R, we need to calculate the standard error of the mean (sem) and mean (like you did), and we use arrows() to add the error bar:

Shannon_sem <- tapply(df$Shannon.index,df$type,function(x)sd(x)/sqrt(length(x)))
Shannon_mean <- tapply(df$Shannon.index,df$type,mean)
YMAX <-ceiling(max(Shannon_mean+Shannon_sem))

PLOT <- barplot(Shannon_mean,
xlab = "Environment", ylab = "Shannon Diversity Index", 
main = "Diversity of Parasites \nfound on Fish from River 
and Lake Environments", cex.main = 1, col = "gray80",
ylim = c(0,YMAX))
arrows(x0=PLOT,y0=Shannon_mean+Shannon_sem,cex.main=0.7,
y1=Shannon_mean-Shannon_sem,angle=90,code=3,length=0.1)

enter image description here

If you use ggplot2:

library(ggplot2)
ggplot(df,aes(x=type,y=Shannon.index)) + stat_summary(fun.y=mean,geom="bar",fill="gray80") + 
theme_bw() + 
stat_summary(fun.data = mean_se, geom = "errorbar",width=0.2)

You can calculate the mean and standard error on the fly, using stat_summary()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Using ggplot2 and the standard deviation to create the errorbars:

library(ggplot2)

# I'm just making up the numbers to provide the solution
mean_river <- 25 # mean(parasite_data$Shannon.index[1:24])
mean_lake <- 29 # mean(parasite_data$Shannon.index[25:43])

# To plot the error bars i'm assuming you want the standard deviation
# but you can use the min or max value
sd_river <- 0.5 # sd(parasite_data$Shannon.index[1:24])
sd_lake <- 4 # sd(parasite_data$Shannon.index[25:43])

# Instead of matrix I would use a data.frame for ggplot
Shannon_data <- data.frame(name = c("River", "Lake"),
                           mean = c(mean_river, mean_lake),
                           sd = c(sd_river, sd_lake)) 

ggplot(Shannon_data) +
  geom_bar(aes(x=name, y=mean), stat="identity", fill="gray80") +
  geom_errorbar(aes(x=name, ymin=mean-sd, ymax=mean+sd), 
                width=0.4, colour="orange", alpha=0.9, size=1.3) +
  labs(title = "Diversity of Parasites found on Fish from River and Lake Environments") + 
  xlab("Environment") + ylab("Shannon Diversity Index") + theme_bw()

This would generate the next plot: Ggplot with errorbars with std

Using the minimum and maximum value to create the errorbars:

# Example with max and min value instead of sd
min_river <- 22 # min(parasite_data$Shannon.index[1:24])
min_lake <- 21 # min(parasite_data$Shannon.index[25:43])
max_river <- 31 # max(parasite_data$Shannon.index[1:24])
max_lake <- 30 # max(parasite_data$Shannon.index[25:43])

Shannon_data <- data.frame(name = c("River", "Lake"),
                           mean = c(mean_river, mean_lake),
                           min = c(min_river, min_lake), 
                           max = c(max_river, max_lake)) 
ggplot(Shannon_data) +
  geom_bar(aes(x=name, y=mean), stat="identity", fill="gray80") +
  geom_errorbar(aes(x=name, ymin=min, ymax=max), 
                 width=0.4, colour="orange", alpha=0.9, size=1.3) +
  labs(title = "Diversity of Parasites found on Fish from River and Lake Environments") + 
  xlab("Environment") + ylab("Shannon Diversity Index") + theme_bw()

ggplot errorbars with min and max

Hope this help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.