R 同一绘图中x范围不同部分的标准误差绘图平均值

R 同一绘图中x范围不同部分的标准误差绘图平均值,r,plot,R,Plot,我想给出我的图中x范围各个部分y值的平均值和标准误差。i、 e.灰色垂直线之间。我已经在谷歌上搜索过了,并且在没有帮助的情况下检查了所有以前的答案 数据帧: dput(data) structure(list(Year = c(1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 19

我想给出我的图中x范围各个部分y值的平均值和标准误差。i、 e.灰色垂直线之间。我已经在谷歌上搜索过了,并且在没有帮助的情况下检查了所有以前的答案

数据帧:

dput(data) structure(list(Year = c(1970, 1971, 1972, 1973, 1974, 1975, 1976, 
1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 
1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 2016), Tot = c(52, 46, 19, 
52, 94, 102, 104, 143, 117, 146, 135, 132, 152, 134, 138, 141, 
134, 134, 134, 145, 152, 126, 139, 138, 97, 120, 97, 98, 76, 
84, 102, 98, 99, 96, 96, 98, 102, 102, 107, 109, 109, 107, 112, 
107, 93, 114, 108)), row.names = c(NA, -47L), class = c("tbl_df", 
"tbl", "data.frame"))
绘图脚本:

plot(data$Year,data$Tot,type="b",xlab="" ,ylab="",
 ylim=c(0,200),lty="solid",lwd=2,col='black',cex.lab=1.5,las=1,cex=1.5)
abline(v=1994,col="grey",lty="dotted",lwd=3)
abline(v=2005,col="grey",lty="dotted",lwd=3)
abline(v=2010,col="grey",lty="dotted",lwd=3)
打印输出加上手动绘制的平均值和标准误差阴影:

编辑以反映OP的修订帖子

# Calculate standard errors for each year range
se1970 <- sd(data$Tot[data$Year >= 1970 & data$Year < 1994]) / sqrt(length(data$Tot[data$Year >= 1970 & data$Year < 1994]))
se1994 <- sd(data$Tot[data$Year >= 1994 & data$Year < 2005]) / sqrt(length(data$Tot[data$Year >= 1994 & data$Year < 2005]))
se2005 <- sd(data$Tot[data$Year >= 2005 & data$Year < 2010]) / sqrt(length(data$Tot[data$Year >= 2005 & data$Year < 2010]))
se2010 <- sd(data$Tot[data$Year >= 2010]) / sqrt(length(data$Tot[data$Year >= 2010]))

# Calculate means for each year range
mean1970 <- mean(data$Tot[data$Year >= 1970 & data$Year < 1994])
mean1994 <- mean(data$Tot[data$Year >= 1994 & data$Year < 2005])
mean2005 <- mean(data$Tot[data$Year >= 2005 & data$Year < 2010])
mean2010 <- mean(data$Tot[data$Year >= 2010])

# 1970-1993
polygon(x = c(1970, 1970, 1993, 1993),
        y = c(mean1970 + se1970, mean1970 - se1970, mean1970 - se1970, mean1970 + se1970),
        col = alpha("#4286f4", .25))
lines(x = 1970:1993, y = rep(mean1970, length(1970:1993)),
      col = "red", lwd = 2)

# 1993-2004
polygon(x = c(1994, 1994, 2004, 2004),
        y = c(mean1994 + se1994, mean1994 - se1994, mean1994 - se1994, mean1994 + se1994),
        col = alpha("#4286f4", .25))
lines(x = 1994:2004, y = rep(mean1994, length(1994:2004)),
      col = "red", lwd = 2)

# 2005-2009
polygon(x = c(2005, 2005, 2009, 2009),
        y = c(mean2005 + se2005, mean2005 - se2005, mean2005 - se2005, mean2005 + se2005),
        col = alpha("#4286f4", .25))
lines(x = 2005:2009, y = rep(mean2005, length(2005:2009)),
      col = "red", lwd = 2)

# 2010-2016
polygon(x = c(2010, 2010, 2016, 2016),
        y = c(mean2010 + se2010, mean2010 - se2010, mean2010 - se2010, mean2010 + se2010),
        col = alpha("#4286f4", .25))
lines(x = 2010:2016, y = rep(mean2010, length(2010:2016)),
      col = "red", lwd = 2)
#计算每个年度范围的标准误差
se1970=1970和数据$Year<1994])/sqrt(长度(数据$Tot[数据$Year>=1970和数据$Year<1994]))
se1994=1994年&数据$Year<2005])/sqrt(长度(数据$Tot[数据$Year>=1994年&数据$Year<2005]))
se2005=2005和数据$Year<2010])/sqrt(长度(数据$Tot[数据$Year>=2005和数据$Year<2010]))
se2010=2010])/sqrt(长度(数据$Tot[数据$Year>=2010]))
#计算每年范围内的平均数
平均1970年=1970年&数据$年<1994年])
平均1994年=1994年&数据$年<2005年])
2005年平均值=2005年&数据$Year<2010年])
平均值2010=2010])
# 1970-1993
多边形(x=c(1970、1970、1993、1993),
y=c(平均1970年+se1970年,平均1970年-se1970年,平均1970年-se1970年,平均1970年+se1970年),
col=alpha(#4286f4,.25))
线条(x=1970:1993,y=rep(平均1970,长度(1970:1993)),
col=“红色”,lwd=2)
# 1993-2004
多边形(x=c(1994、1994、2004、2004),
y=c(平均1994年+1994年,平均1994年-1994年,平均1994年-1994年,平均1994年+1994年),
col=alpha(#4286f4,.25))
直线(x=1994:2004,y=rep(平均值1994,长度(1994:2004)),
col=“红色”,lwd=2)
# 2005-2009
多边形(x=c(2005、2005、2009、2009),
y=c(平均值2005+se2005,平均值2005-se2005,平均值2005-se2005,平均值2005+se2005),
col=alpha(#4286f4,.25))
直线(x=2005:2009,y=rep(平均值2005,长度(2005:2009)),
col=“红色”,lwd=2)
# 2010-2016
多边形(x=c(2010、2010、2016、2016),
y=c(平均2010年+2010年12月,平均2010年-2010年12月,平均2010年-2010年12月,平均2010年+2010年12月),
col=alpha(#4286f4,.25))
线条(x=2010:2016,y=rep(平均值2010,长度(2010:2016)),
col=“红色”,lwd=2)

请键入
dput(数据)
并将结果粘贴到您的问题中,以便我们可以使用您的数据集进行答复。谢谢,但请参阅编辑后的文章,其中更好地解释了我的目的。太棒了!完美的谢谢