添加文字格子情节?(Add text to lattice plot?)

2019-09-22 03:00发布

有人能告诉我如何将文本行添加到格子的情节。

我的代码是:

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

这里pane=mypanel是增加一个abline。 我想连翘醇提物和夫之间的线性回归方程添加到他们的阴谋。

谢谢!

Answer 1:

所以,用可再生的数据:

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



Answer 2:

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



文章来源: Add text to lattice plot?
标签: r lattice