R 如何获得ggplot2中对数图中回归线的方程式?
我有一个对数图,我使用以下方法得到回归线:R 如何获得ggplot2中对数图中回归线的方程式?,r,ggplot2,equation,non-linear-regression,R,Ggplot2,Equation,Non Linear Regression,我有一个对数图,我使用以下方法得到回归线: geom_smooth(formula = y ~ x, method='lm') 但是现在我想得到这一行的方程(例如y=a*x^(-b))并打印出来。我设法在林林的阴谋中得到了它,但这次没有。 代码如下: mydataS<-data.frame(DurPeak_h[],IntPeak[],IntPeakxDurPeak[],ID[]) #df peak names(mydataS)<-c("x","y","ID","IDEVENT")
geom_smooth(formula = y ~ x, method='lm')
但是现在我想得到这一行的方程(例如y=a*x^(-b))并打印出来。我设法在林林的阴谋中得到了它,但这次没有。
代码如下:
mydataS<-data.frame(DurPeak_h[],IntPeak[],IntPeakxDurPeak[],ID[]) #df peak
names(mydataS)<-c("x","y","ID","IDEVENT")
plotID<-ggplot(mydataS, aes(x=x, y=y, label=IDEVENT)) +
geom_text(check_overlap = TRUE, hjust = 0, nudge_x = 0.02)+
geom_point(colour="black", size = 2) + geom_point(aes(colour = ID)) +
geom_quantile(quantiles = qs, colour="green")+
scale_colour_gradient(low = "white", high="red") +
scale_x_log10(limits = c(min(DurEnd_h),max(DurEnd_h))) +
scale_y_log10(limits = c(min(IntEnd),max(IntEnd))) +
geom_smooth(formula = y ~ x, method='lm')
ggsave(height=7,"plot.pdf")
mydataSmydataS采用了一种变通方法:使用nls
精确计算两个参数a和b:
nlsPeak <- coef(nls(y ~ a*(x)^b, data = mydataS, start = list(a=30, b=-0.1)))
其名称如下:
annotate("text",x = 3, y = 180,label = power_eqn(mydataS), parse=TRUE, col="black") +
希望有帮助 也许最简单的方法是在Ggplot之外拟合模型,你认为方程来自哪里?不要让你的生活变得更艰难,只需自己拟合lm
<代码>ggplot2
不是建模包。
power_eqn = function(ds){
m = nls(y ~ a*x^b, start = list(a=30, b=-0.1), data = ds);
eq <- substitute(italic(y) == a ~italic(x)^b,
list(a = format(coef(m)[1], digits = 4),
b = format(coef(m)[2], digits = 2)))
as.character(as.expression(eq));
}
annotate("text",x = 3, y = 180,label = power_eqn(mydataS), parse=TRUE, col="black") +