如何在ggplot2中的log-log图中得到回归线的等式?

时间:2017-01-04 08:47:15

标签: r ggplot2 equation non-linear-regression

我有一个对数日志图,我通过使用得到了回归线:

geom_smooth(formula = y ~ x, method='lm') 

但是现在我想获得这一行的等式(例如 y = a * x ^( - b))并打印出来。我设法在lin-lin情节中得到它但在这种情况下不是。 这是代码:

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")

2 个答案:

答案 0 :(得分:0)

mydataS<-data.frame(DurPeak_h[],IntPeak[],IntPeakxDurPeak[],ID[])
names(mydataS)<-c("x","y","ID","IDEVENT")
model <- lm(y~x, header = T)
summary(model)

使用截距值为“b”,系数为“a”

答案 1 :(得分:0)

是否采用了解决方法:使用nls计算两个参数 a b ,正好:

nlsPeak <- coef(nls(y ~ a*(x)^b, data = mydataS, start = list(a=30, b=-0.1)))

然后使用annotate绘制线条(参见一些示例here),最后使用函数打印等式:

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") +

希望它有所帮助!