如何在图形上打印一些回归信息

时间:2019-04-12 21:59:43

标签: r

我有这样的数据

df<- structure(list(How = c(3.1e-05, 0.000114, 0.000417, 0.00153, 
0.00561, 0.0206, 0.0754, 0.277, 1.01, 3.72), Where = c(1, 0.948118156866697, 
0.920303987764611, 1.03610743904536, 1.08332987533419, 0.960086785898477, 
0.765642506120658, 0.572520170014998, 0.375835106792894, 0.254180720963181
)), class = "data.frame", row.names = c(NA, -10L))

library(drc)

我使我的模型像这样

fit <- drm(formula = Where ~ How, data = df, 
           fct = LL.4(names=c("Slope","Lower Limit","Upper Limit", "EC50")))

然后我这样绘制它

plot(NULL, xlim = c(0.000001, 4), ylim = c(0.01, 1.2),log = "x")
  points(df$How, df$Where, pch = 20)
  x1 = seq(0.000001, 4, by=0.0001)
  y1 = coef(fit)[3] + (coef(fit)[2] - coef(fit)[3])/(1+(x1/coef(fit)[4])^((-1)*coef(fit)[1]))
  lines(x1,y1)

现在,我希望能够在图中打印以下信息

max(df$How)
min(df$How)
coef(fit)[2]
coef(fit)[3]
(-1)*coef(fit)[1]
coef(fit)[4]

我试图这样做

text(labels = bquote(FirstT~"="~.(round(max(df$How)))))
text(labels = bquote(SecondT~"="~.(round(min(df$How))))
text(labels = bquote(A[min]~"="~.(round(coef(fit)[2]))))
text(labels = bquote(A[max]~"="~.(coef(fit)[3]))))    
text(labels = paste0("Slope = ", round((-1)*coef(fit)[1])))

这当然不起作用。我更喜欢自动在图的左上角找到打印这些信息的地方

1 个答案:

答案 0 :(得分:1)

在下面的代码中,我们使用par("usr")获取绘图区域坐标范围,然后使用这些坐标区域和数据点位置将标签自动放置在所需位置。

# Reduce margins
par(mar=c(5,4,0.5,0.5))

# Get extreme coordinates of plot area
p = par("usr")
p[1:2] = 10^p[1:2] # Because xscale is logged

text(max(df$How), df$Where[which.max(df$How)], 
     labels = bquote(FirstT~"="~.(round(max(df$How)))), pos=1)
text(min(df$How), df$Where[which.min(df$How)], 
     labels = bquote(SecondT~"="~.(round(min(df$How)))), pos=1)
text(1.1*p[1], p[3] + 0.02*diff(p[3:4]), 
     labels = bquote(A[min]~"="~.(round(coef(fit)[2]))), adj=c(0,0))

enter image description here