[R] 2 Trellis graphics question
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed Jun 13 00:28:44 CEST 2007
On 6/12/07, Alan S Barnett <asb at mail.nih.gov> wrote:
> 1) I have a data that includes some "bad" data. I want to make a
> trellis plot where each panel contains
> a) A scatter plot of the "good" data
> b) A scatter plot of the "bad" data in a different color
> c) A best fit line of all the data, and
> d) A best fit line of the "good" data.
>
> I tried using xyplot and setting the "group" argument, but I'm having
> trouble. Here is my code:
>
> xyplot(y ~ x | status, data=data,groups=good,
> + panel=function(x,y,...){
> + panel.xyplot(x,y,...)
> + panel.lmline(x,y,col = "red")
> + panel.lmline(x[good],y[good],col = "blue")
> + }
> + )
You are close, except the last panel.lmline call is not meaningful
because the 'x' and 'y' in the panel function are not the same length
as 'good'. You need to use 'subscripts' for that.
To fix ideas, here's a concrete example (I've changed the names 'x'
and 'y' to 'xx' and 'yy' to avoid any confusion):
mydata <-
data.frame(xx = sample(100),
yy = rnorm(100) + rep(c(5, 10), c(80, 20)),
status = gl(5, 1, 100),
good = rep(c(TRUE, FALSE), c(80, 20)))
Then, what you want can be achieved with:
xyplot(yy ~ xx | status, mydata, groups = good,
panel = function(x, y, groups, subscripts, ...) {
panel.xyplot(x, y,
groups = groups,
subscripts = subscripts,
...)
panel.lmline(x, y, col = "red")
good.id <- groups[subscripts]
## good.id: subset of 'good' relevant for this panel
panel.lmline(x[good.id], y[good.id], col = "blue")
})
This also works if 'good' is globally visible and not in 'mydata'.
[...]
> 2) There are 5 different values of status, but I only want to plot three
> of them. Can I do this without copying only the desired elements into a
> new data frame?
Sure, just use the additional argument
subset = (status %in% c("1", "3", "5"))
or whatever the appropriate subset is.
-Deepayan
More information about the R-help
mailing list