3

有人可以告诉我如何在格子图中添加文本行。

我的代码是:

xyplot(Neff~Eeff,data=phuong,panel=mypanel,
       col="black",
       pch=18,xlab="Energy efficiency (%)",
       ylab = "Nitrogen efficiency (%)", main="(a)")

这里pane=mypanel是添加一个abline。我想在他们的图中添加 Eeff 和 Neff 之间的线性回归方程。

谢谢!

4

2 回答 2

8

因此,使用可重现的数据:

set.seed(1)

phuong <- data.frame(Eeff = rnorm(100,31))
phuong$Neff <- rnorm(100,19.7 + .36*phuong$Eeff)

# Create the lm object ahead of time
lm1 <- lm(Neff ~ Eeff, data = phuong)

# Create the character string that you want to print
tp <- sprintf("%s=%.1f + %.2f %s", all.vars(formula(lm1))[1],
  coef(lm1)[1], coef(lm1)[2], all.vars(formula(lm1))[2])

# Change the mypanel function to use the lm1 object
mypanel<-function(x,y,...){
  panel.xyplot(x, y, ...)
  panel.abline(lm1)
  panel.text(30,33,labels=tp)
}

library(lattice)

# and off we go.

xyplot(Neff~Eeff,data=phuong,panel=mypanel,
       col="black",
       pch=18,xlab="Energy efficiency (%)",
       ylab = "Nitrogen efficiency (%)", main="(a)")

在此处输入图像描述

于 2012-09-03T17:09:23.813 回答
4
library(latticeExtra)

set.seed(1)

phuong <- data.frame(Eeff = rnorm(100,31))
phuong$Neff <- rnorm(100,19.7 + .36*phuong$Eeff)

xyplot(Neff ~ Eeff, data= phuong) + 
  layer(panel.ablineq(lm(Neff ~ Eeff, data= phuong), r.sq= TRUE, rot = TRUE))

在此处输入图像描述

于 2015-04-11T07:34:18.993 回答