首页 > 解决方案 > 如何在图形上打印一些回归信息

问题描述

我有这样的数据

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

这当然行不通。我更喜欢一种自动的方式来找到打印这些信息的图的左上角的位置

标签: r

解决方案


在下面的代码中,我们获取绘图区域坐标范围,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))

在此处输入图像描述


推荐阅读