Eco*_*o06 3 plot r mean linear-regression gplots
我的问题是我想用平均值和标准差创建图.
我plotmeans在包中找到了这个函数,gplots现在我想在该图中添加一个趋势线.我尝试这样做,abline但它不起作用.我真的很感激一些帮助.
我的数据如下:
year <- c(2004, 2004, 2004, 2005, 2005, 2005, 2006, 2006, 2006, 2007, 2007, 2007, 2008, 2008, 2008, 2009, 2009, 2009)
value <- c(116,114,115,110,110,109,100,99,102,95,96,95,90,91,94,70,71,73)
df <- data.frame(year, value); head(df)
library(gplots)
plotmeans(value ~ year, data= df, p = 0.99, connect= FALSE)
abline(h=c(100), col= "blue", lty= 2)
abline(lm(value ~ year, data= df))
Run Code Online (Sandbox Code Playgroud)
所以第一个abline工作正常,但lm线不行.
在图中,x轴上的值被重新编码为因子水平.因此,您必须转换year为因子并使用其数值.结果用于该lm功能.
df$year2 <- as.numeric(factor(year))
library(gplots)
plotmeans(value ~ year, data= df, p = 0.99, connect= FALSE)
abline(h=c(100), col= "blue", lty= 2)
abline(lm(value ~ year2, data= df))
Run Code Online (Sandbox Code Playgroud)
