R: Many matrix grid levels

My goal is to build a levelplot (from a lattice package) with 4 or more separate sections using the same colorkey. Although this is apparently relatively simple using functions, I could not find a solution using data matrices. One-matrix working level example:

d <- replicate(10,rnorm(10))
levelplot(d)

I found partial solutions that use print and split to show all 4 levels of a level on one screen, but this will require me to either turn off colorkey or show it on each plot. None of the solutions are completely satisfactory.

If I use layout-option in levelplot, for example:

levelplot(d, layout=c(2,2))

I get the desired layout with one big colorkey, main and xlab / ylab, but prints only one level.

I am trying to build a formula that gives the desired result, but I'm afraid that my understanding of data frames, arrays and matrices is not deep enough for this. If anyone knows about a working solution, I would be very grateful. I think this is something like a string (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 matrices is not an option. Some of them are derived from raw text files.

Thanks in advance,

-JP

+5
source share
1 answer

Using ggplotand reshapealong with the list d:

require(reshape)
require(ggplot2)

ggplot(melt(d), aes(x=X1, y=X2)) +
  facet_wrap(~ L1, ncol=2) +
  geom_tile(aes(fill=value)) +
  coord_equal()

What gives:

enter image description here

+7
source

All Articles