[R] lattice: How to plot an xyplot (with panels) without using a dataframe?
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed May 19 15:25:58 CEST 2010
On Wed, May 19, 2010 at 5:45 PM, Marius Hofert <m_hofert at web.de> wrote:
> Dear R users,
>
> Is it possible to draw an xyplot (example below) without having to build a dataframe?
Yes, for example
x = 1:10
y = rnorm(10)
g = gl(2, 5)
xyplot(y ~ x | g)
But that is not really your question.
> For example: I have some data in matrix-form. Usually, the first column is the x-value, the columns
> 2 and 3 are y-values. I would then like to plot 2 panels where the first panel draws the second column
> against the first column and the second panel draws the third column against the first column. It
> seems "tedious" to construct a dataframe just for the plots.
Someone needs to do that tedious task. If you want to automate it,
write a matrix method for xyplot():
xyplot.matrix <- function(x, ..., outer = TRUE)
{
x <- as.data.frame(x)
stopifnot(nrow(x) > 1)
nms <- sprintf("`%s`", colnames(x))
form <- sprintf("%s ~ %s", paste(nms[-1], collapse = "+"), nms[1])
xyplot(as.formula(form), data = x, ..., outer = outer)
}
x <- 1:10
y <- cbind(x,-x) # 2-column matrix
p <- cbind(rep(1,10),rep(2,10)) # "panel" matrix
xyplot(y)
xyplot(cbind(x, p))
-Deepayan
> Cheers,
>
> Marius
>
> library(lattice)
>
> x <- 1:10
> y <- cbind(x,-x) # 2-column matrix
> p <- cbind(rep(1,10),rep(2,10)) # "panel" matrix
>
> xyplot(y~x|p) # does not work; can this be fixed (without the use of a dataframe)?
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list