我正在为我所做的研究绘制多样性指数均值的柱状图。我已经计算出每个样本的索引,并将其添加到我的表中(我通常会读入该表)。然后,我计算了两种不同环境的指数均值并绘制了这些均值。但是,我无法解决如何添加错误栏的问题。我了解ggplot2是执行此操作的有用工具,但是无法理解我的解释。
SO,基本上是尝试从表中取两个值的平均值,并绘制带有误差线的条形图。这是我目前的代码
`mean_river <- mean(parasite_data$Shannon.index[1:24])
mean_lake <- mean(parasite_data$Shannon.index[25:43])`
Shannon_mean <- matrix(c(mean_river, mean_lake),
nrow = 1, ncol = 2,
dimnames = list(c("mean"), c("River","Lake")))
`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")`
就像我说的那样,我曾尝试使用ggplot,但无法正确读取数据。任何帮助,将不胜感激。
答案 0 :(得分:0)
使用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()
使用最小值和最大值创建误差线:
# 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()
希望获得帮助!
答案 1 :(得分:0)
我模拟了一些看起来像您的数据,因此您无需将它们放入单独的向量中。将它们保存在数据框中
Shannon.index <- runif(43,1.5,3.5)
type = rep(c("River","Lake"),times=c(24,19))
df <- data.frame(Shannon.index,type)
对于基于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)
如果您使用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)
您可以使用stat_summary()