我正在尝试将两个沉积物核心进行相关性分析,因为在核心中不同深度处具有不同的样本。我使用了ggplot2函数绘制了一个5次多项式回归图,显示了方程和r2值。
我的问题是方程本身,r2值是正确的,但方程不正确。我认为这与lm_eq引用线性回归有关,但我不太确定。
非常感谢任何帮助或指导。我对图形本身感到满意,但是对于如何清理我的代码的任何建议也将不胜感激。
我已经尝试了搜索其他函数以显示方程,但没有找到解决方案。
long_data <- gather(Correlations, key = "Core", value = "Depth",
#Reshapes my data frame
LC1U, LC3U)
df <- data.frame("x"=long_data$Sample, "y"=long_data$Depth)
lm_eqn = function(df){ m=lm(y ~ poly(x, 5), df)#3rd degree polynomial eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 4))) as.character(as.expression(eq)) }
p1 <- ggplot(long_data, aes(x=Sample,y=Depth)) + geom_point(aes(color=Core)) +
labs(x ='Sample N.', y ='Depth (mm)', title = 'Core Correlation of Lake Nganoke') +
ylim(1,800)
p1 + stat_smooth(method = "lm", formula = y~poly(x,5, raw = TRUE), size = 1) +
annotate("text", x = 0, y = 800, label = lm_eqn(df), hjust=0, family="Times", parse = TRUE) + #Add polynomial regression
scale_y_continuous(trans = "reverse", breaks = c(0,100,200,300,400,500,600,700,800))
raw
设置为TRUE。lm(y ~ poly(x, 5, raw = TRUE), data = df)
- Tony Ladson