使用 R 中绘制的标准误差创建条形图

Creating barplot with standard errors plotted in R

我正在尝试找到在 R 中创建条形图并显示标准错误的最佳方法。我看过其他文章,但我无法弄清楚与我自己的数据一起使用的代码(之前没有使用过 ggplot,这似乎是最常用的方式,而 barplot 不与数据帧合作)。我需要在两种情况下使用它,为此我创建了两个示例数据框:

绘制 df1,使 x 轴具有站点 a-c,y 轴显示 V1 的平均值并突出显示标准误差,类似于此 example 灰色。在这里,植物生物量应该是平均 V1 值,处理应该是我的每个站点。

以相同的方式绘制 df2,但前后以类似于 this 的方式彼此相邻,因此预测试和 post-测试等同于之前在我的例子之后。

x <- factor(LETTERS[1:3])
site <- rep(x, each = 8)
values <- as.data.frame(matrix(sample(0:10, 3*8, replace=TRUE), ncol=1))
df1 <- cbind(site,values)
z <- factor(c("Before","After"))
when <- rep(z, each = 4)
df2 <- data.frame(when,df1)

对于更有经验的 R 用户,尤其是那些使用 ggplot 的用户,我很抱歉很简单,但我无法将我在其他地方找到的代码片段应用到我的数据中。我什至无法获得足够的代码来生成图表的开始,所以我希望我的描述足够了。先感谢您。

是这样的吗?

library(ggplot2)
get.se <- function(y) {
 se <- sd(y)/sqrt(length(y))
 mu <- mean(y)
 c(ymin=mu-se, ymax=mu+se)
}
ggplot(df1, aes(x=site, y=V1)) +
  stat_summary(fun.y=mean, geom="bar", fill="lightgreen", color="grey70")+
  stat_summary(fun.data=get.se, geom="errorbar", width=0.1)

ggplot(df2, aes(x=site, y=V1, fill=when)) +
  stat_summary(fun.y=mean, geom="bar", position="dodge", color="grey70")+
  stat_summary(fun.data=get.se, geom="errorbar", width=0.1, position=position_dodge(width=0.9))

因此,这利用了 ggplot 中的 stat_summary(...) 函数,首先,使用 mean(...)(对于条形图)总结给定 xy,然后使用误差线的 get.se(...) 函数为给定的 x 总结 y。另一种选择是在使用 ggplot 之前总结您的数据,然后使用 geom_bar(...)geom_errorbar(...).

此外,绘制 +/- 1 se 并不是一个很好的做法(尽管它经常被使用)。你最好绘制合法的置信度限制,你可以这样做,例如,使用内置的 mean_cl_normal 函数而不是人为的 get.se(...)mean_cl_normal returns 基于数据呈正态分布假设的 95% 置信限(或者您可以将 CL 设置为其他值;阅读 documentation)。

我为此使用了 group_bysummarise_each function 以及包 plotrix

中的 std.error 函数
library(plotrix) # for std error function
library(dplyr) # for group_by and summarise_each function
library(ggplot2) # for creating ggplot

对于 df1 图

# Group data by when and site
grouped_df1<-group_by(df1,site)

#summarise grouped data and calculate mean and standard error using function mean and std.error(from plotrix)
summarised_df1<-summarise_each(grouped_df1,funs(mean=mean,std_error=std.error))


# Define the top and bottom of the errorbars
limits <- aes(ymax = mean + std_error, ymin=mean-std_error)

#Begin your ggplot
#Here we are plotting site vs mean and filling by another factor variable when
g<-ggplot(summarised_df1,aes(site,mean))

#Creating bar to show the factor variable position_dodge 
#ensures side by side creation of factor bars
g<-g+geom_bar(stat = "identity",position = position_dodge())

#creation of error bar
g<-g+geom_errorbar(limits,width=0.25,position = position_dodge(width = 0.9))
#print graph
g

对于 df2 图

# Group data by when and site
grouped_df2<-group_by(df2,when,site)

#summarise grouped data and calculate mean and standard error using function mean and std.error
summarised_df2<-summarise_each(grouped_df2,funs(mean=mean,std_error=std.error))

# Define the top and bottom of the errorbars
limits <- aes(ymax = mean + std_error, ymin=mean-std_error)

#Begin your ggplot
#Here we are plotting site vs mean and filling by another factor variable when
g<-ggplot(summarised_df2,aes(site,mean,fill=when))

#Creating bar to show the factor variable position_dodge 
#ensures side by side creation of factor bars
g<-g+geom_bar(stat = "identity",position = position_dodge())

#creation of error bar
g<-g+geom_errorbar(limits,width=0.25,position = position_dodge(width = 0.9))
#print graph
g