简体   繁体   English

使用 ggplot 将误差线添加到 Barplot

[英]using ggplot to add error bars to Barplot

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.但是,我无法弄清楚如何添加误差线。我知道 ggplot2 是一个有用的工具,但无法理解解释。

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.就像我说的,我尝试使用 ggplot,但无法正确读取数据。 any help would be appreciated.任何帮助,将不胜感激。

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:对于基数 R 的条形图,我们需要计算均值 (sem) 和均值(就像您所做的)的标准误差,我们使用arrows()添加误差条:

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)

在此处输入图片说明

If you use ggplot2:如果您使用 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()您可以使用stat_summary()即时计算平均值和标准误差

在此处输入图片说明

Using ggplot2 and the standard deviation to create the errorbars:使用ggplot2和标准偏差来创建误差条:

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:这将生成下一个图: 带有 std 错误条的 Ggplot

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 误差条

Hope this help!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM