xyplot中的多个ablines [英] Multiple ablines in xyplot
问题描述
我有一个长"数据帧,定义为:
I have a "long" dataframe defined as:
q <- data.frame(Indicator.Code=factor(),Year=numeric(),Value=numeric())
并尝试在单个xyplot中将值作为年份的函数针对每个不同的Indicator.Code
,如下所示
and am trying to plot in a single xyplot the values as a function of the year, for each different Indicator.Code
, as follows
xyplot( Value~Year,data=q,group=Indicator.Code)
到目前为止,太好了.现在,我尝试添加与线性回归相对应的行
So far, so good. Now I am trying to add lines corresponding to the linear regressions
rlm(q$Value[q$Indicator.Code==a]~q$Year[q$Indicator.Code==a])
Indicator.Code
的所有值.
我不知道该怎么做.添加回归线的常用方法,即
I do not know how to do it. The usual way to add regression lines, i.e
xyplot( Value~Year,data=q,group=Indicator.Code),
panel = function(x, y) {
panel.xyplot(x, y)
panel.abline(rlm(y ~ x))
}))
无法正常工作(它为整个数据集计算了一次回归,并添加了一条回归线).此外,我已经计算了回归(除了图形以外,我也需要回归),并且讨厌必须重新计算回归的想法.
does not work properly (it computes a single regression, and adds a single regression line, for the whole dataset). Besides, I have already computed the regressions (I need them for things other than graphics too), and hate the idea of having to recompute them.
有人暗示新手可以跟进吗?
Any hints a novice could follow?
推荐答案
对于为每个group
绘制单独符号的自定义面板函数,点阵要求您将实际的面板函数包装在对panel.superpose()
的调用中.这是一个使用mtcars
data.frame中的数据的示例.
For a customized panel function that plots separate symbols for each group
, lattice requires that you wrap the actual panel function in a call to panel.superpose()
. Here's an example, using data in the mtcars
data.frame.
library(lattice)
library(MASS)
myPanel <- function(x,y,...) {
panel.xyplot(x,y,...)
panel.abline(rlm(y~x), ...)
}
xyplot(mpg~disp, group = cyl, data = mtcars,
panel = function(...) panel.superpose(panel.groups = myPanel, ...))
## Or, equivalently:
xyplot(mpg~disp, group = cyl, data = mtcars,
panel = panel.superpose, panel.groups = myPanel)
这篇关于xyplot中的多个ablines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!