library(lattice)
mtcars$transmission <- factor(mtcars$am, levels=c(0,1),
labels=c("Automatic", "Manual"))
panel.smoother <- function(x, y) {
panel.grid(h=-1, v=-1)
panel.xyplot(x, y)
panel.loess(x, y)
panel.abline(h=mean(y), lwd=2, lty=2, col="darkgreen")
}
xyplot(mpg~disp|transmission,data=mtcars,
scales=list(cex=.8, col="red"),
panel=panel.smoother,
xlab="Displacement", ylab="Miles per Gallon",
main="MPG vs Displacement by Transmission Type",
sub = "Dotted lines are Group Means", aspect=1)
在上面的代码中有几个地方需要指出。 panel.xyplot()函数画出了个别点, panel.
loess()函数在每个面板图中画出了非参数拟合曲线。 panel.abline()函数在调解变量的每个水平中添加了水平参考线(mpg的均值)。(如果你用h=mean(mtcars$mpg)代替h=mean(y),在
整个样本中将产生以mpg均值为基础的单个参考线。)scales=选项呈现大小为默认字体80%的红
色刻度注释(坐标轴数字和刻度线)。
暂无数据