gpt4 book ai didi

r - 将错误栏添加到 ggplot 时出错

转载 作者:行者123 更新时间:2023-12-01 12:32:10 25 4
gpt4 key购买 nike

亲爱的 Stackoverflow 用户,

我想绘制一个包含三个带误差线的独立变量的分组条形图。我的图表基于 Stacked Overflow(分组条内的堆叠条)的示例,使用 ggplot 和 geom_bar。当我根据帮助页面的示例添加 geom_errorbar 时,出现以下错误:if (empty(data)) { 中的错误:缺少需要 TRUE/FALSE 的值

这是我使用的脚本:

treatment<-rep(c(rep(c(1),8),rep(c(2),8)),2)
origin<-rep(c("A","B"),16)
time<-c(rep(c(5),16),rep(c(10),16))
sulfide<-c(0,10,5,8,9,6,16,18,20,25,50,46,17,58,39,43,20,25,50,46,17,58,39,43,100,120,103,104,150,160,200,180)

Reed<-data.frame(treatment,origin,time,sulfide)

# specify factor types
Reed$treatment<-as.factor(Reed$treatment)
Reed$origin<-as.character(Reed$origin)
Reed$time<-as.factor(Reed$time)

library(ggplot2)
library(scales)

#draw plot
ggplot() +geom_bar(data=Reed, aes(y = sulfide, x = treatment, fill=origin), stat="identity",position="dodge") +theme_bw() + facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")+ggtitle("Time)")

这是我添加错误栏的方式:

ErrorBars <- function(x, y, upper, lower=upper, length=0.03,...{if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))stop("vectors must be same length")arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)}#function for errorbars

SE<- function(x) sqrt(var(x,na.rm=TRUE)/length(na.omit(x))) #function for SE

Reed$trt<- paste(Reed$treatment,Reed$origin,sep="")#combine treatment and origin to a column
mean_Reed<-data.frame(tapply(Reed$sulfide,list(Reed$trt,Reed$time),mean,na.rm=TRUE)) #mean
SE_Reed<-data.frame(tapply(Reed$sulfide,list(Reed$trt, Reed$time),SE)) # SE

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

#plot with error bars:
ggplot() +geom_bar(data=Reed, aes(y = sulfide, x = treatment, fill=origin), stat="identity",position="dodge") +theme_bw() + facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")+ggtitle("Time)"+ geom_errorbar(limits, width=.2,position="dodge")

我真的找不到我做错了什么。我希望你能帮助我:)

最佳答案

暂时不考虑错误栏的问题,您的情节存在更严重的问题。 treatmenttimeorigin 各有 2 个值,总共有 8 种组合,但硫化物有 32 个值 - 所以有每种组合的 4 个硫化物值。当您使用例如

绘制此图时
ggplot(data=Reed) +
geom_bar(aes(y = sulfide, x = treatment, fill=origin), stat="identity",position="dodge") +
facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")

您正在为 所有四个 硫化物值绘制条形图,它们都以相同的颜色相互叠加。这具有仅显示最大值的效果。很难相信这是您的意图,即使您这样做了,也有更好的方法来做到这一点。例如,如果您想为每个因素组合绘制硫化物的平均值,您可以这样做。

ggp <- ggplot(data=Reed, aes(y = sulfide, x = as.factor(treatment), group=origin)) +
geom_bar(aes(fill=origin), stat="summary", fun.y=mean, position="dodge") +
theme_bw() +
facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")+ggtitle("Time")
ggp

这使用 stat="summary" 使用聚合函数 mean (fun.y=mean) 自动汇总结果。

因为可以使用类似的方法非常简单地添加误差线:

se <- function(y) sd(y)/length(y)   # to calculate standard error in the mean
ggp+stat_summary(geom="errorbar",position=position_dodge(width=0.85),
fun.data=function(y)c(ymin=mean(y)-se(y),ymax=mean(y)+se(y)), width=0.1)

请注意,无需在外部聚合数据 - ggplot 会为您完成。

最后,这种方法适用于使用许多内置函数来生成具有更严格统计的置信限度。

ggp+stat_summary(fun.data=mean_cl_normal, conf.int=0.95,
geom="errorbar",position=position_dodge(width=0.85), width=0.1)

所以在这里我们使用 ggplot 内置函数 mean_cl_normal 来计算均值的 95% 置信限,假设数据服从正态分布(因此,均值服从 t 分布)。我们使用参数 conf.int=... 来指定所需的置信区间,但默认值为 0.95,因此在本例中确实没有必要。

这种类型还有其他几个函数:参见 the documentation以及其中的解释链接。

关于r - 将错误栏添加到 ggplot 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32349230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com