可以给 scatter.smooth 添加标题吗?

Is is possible to add a title to scatter.smooth?

我经常使用 scatter.smooth 函数,但我想知道是否可以直接向该函数添加 titlemain 参数。我阅读了该功能的描述,但没有找到这种可能性。我知道还有其他方法可以做到这一点,但如果可能的话我想保留这个。

d <- data.frame(x = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE), 
                y = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE),
                z = rnorm(500, 20, 4))
mo <- lm(y ~ z, d)
fig <- function(x) {
  scatter.smooth(fitted(x), residuals(x, type = "response"), col = "red")
  abline(0, 0, lty = 2)
  legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo)

您查看 scatter.smooth 的帮助页面,您会看到 ... 参数已传递给 plot。因此,您可以使用 plot 接受的任何参数。还有 main=.

您还可以使用 mtext 向任何图形添加标题,这会在图形边距处添加文本。

所以,你可以这样做:

fig(mo)
mtext("My title", side=3, line=1)

或者修改你的fig函数:

fig <- function(x, ...) {
  scatter.smooth(fitted(x), residuals(x, type = "response"), 
    col = "red", ...)
  abline(0, 0, lty = 2)
  legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo, main="My title")

只需将main添加到smooth函数:

scatter.smooth(x, y, ylab = "Yname", xlab = "Xname", main = "Title")

有效