Jens Peter Andersen / Region Nordjylland
2012-Aug-26 11:57 UTC
[R] Multiple lattice levelplots from matrices
Dear R-users, My goal is to construct a levelplot (from the lattice package) with 4 or more individual plots sharing the same colorkey. While this appears to be relatively simple using functions, I haven't been able to find a solution using data matrices. An example of a working levelplot with just one matrix: d <- replicate(10,rnorm(10)) levelplot(d) I have found partial solutions using print and split to show all 4 levelplots on the same screen, but these would require me to either turn off the colorkey, or show it on every plot. Neither solution is completely satisfactory. If I use the layout-option in levelplot, like so: levelplot(d, layout=c(2,2)) , I get the desired layout, with one large colorkey, main and xlab/ylab, but only one levelplot prints. I have been trying to construct a formula that yields the desired result, but I'm afraid my understanding of data frames, arrays and matrices is not deep enough to do so. If anyone knows of a working solution, I would be very grateful. What I imagine is something along the line of (not working code): d1 <- replicate(10,rnorm(10)) d2 <- replicate(10,rnorm(10)) d3 <- replicate(10,rnorm(10)) d4 <- replicate(10,rnorm(10)) d <- list(d1,d2,d3,d4) di <- c(1,2,3,4) levelplot(x ~ y | di, data = d, layout=c(2,2)) NB! Avoiding the matrices is not an option. Some of them are obtained from raw text files. Thank you in advance, -JP Jens Peter Andersen PhD student, MLIS Medical Library, Aalborg Hospital Aarhus University Hospital
You want array not list, as in levelplot(array(rnorm(400,rep(1:4,each=100)),c(10,10,4))) abind::abind is useful sometimes for binding preexisting matrices to arrays. Cheers On Sun, Aug 26, 2012 at 5:57 AM, Jens Peter Andersen / Region Nordjylland < jepea@rn.dk> wrote:> Dear R-users, > > My goal is to construct a levelplot (from the lattice package) with 4 or > more individual plots sharing the same colorkey. While this appears to be > relatively simple using functions, I haven't been able to find a solution > using data matrices. An example of a working levelplot with just one matrix: > > d <- replicate(10,rnorm(10)) > levelplot(d) > > I have found partial solutions using print and split to show all 4 > levelplots on the same screen, but these would require me to either turn > off the colorkey, or show it on every plot. Neither solution is completely > satisfactory. > > If I use the layout-option in levelplot, like so: > > levelplot(d, layout=c(2,2)) > > , I get the desired layout, with one large colorkey, main and xlab/ylab, > but only one levelplot prints. > > I have been trying to construct a formula that yields the desired result, > but I'm afraid my understanding of data frames, arrays and matrices is not > deep enough to do so. If anyone knows of a working solution, I would be > very grateful. What I imagine is something along the line of (not working > code): > > d1 <- replicate(10,rnorm(10)) > d2 <- replicate(10,rnorm(10)) > d3 <- replicate(10,rnorm(10)) > d4 <- replicate(10,rnorm(10)) > > d <- list(d1,d2,d3,d4) > di <- c(1,2,3,4) > > levelplot(x ~ y | di, data = d, layout=c(2,2)) > > NB! Avoiding the matrices is not an option. Some of them are obtained from > raw text files. > > Thank you in advance, > -JP > > Jens Peter Andersen > PhD student, MLIS > Medical Library, Aalborg Hospital > Aarhus University Hospital > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]