10

I am making a lattice levelplot from x and y factors that range from [0,1]:

      x     y     level                                                                                                                                                                                                                       
1 m3134 m3134 1.0000000                                                                                                                                                                                                                       
2 m3134 m416B 0.4189057                                                                                                                                                                                                                       
3 m416B m3134 0.2696508                                                                                                                                                                                                                       
4 m3134  mA20 0.3322170                                                                                                                                                                                                                       
5  mA20 m3134 0.2454191                                                                                                                                                                                                                       
6 m3134    mB 0.3176792
...

Here is the R script that I use to make the figure from this data:

#!/foo/bar/bin/Rscript --vanilla                                                                                                                                                                        
args <- commandArgs(TRUE)                                                                                                                                                                                                                     
mtxFn <- args[1]                                                                                                                                                                                                                              
pdfFn <- args[2]                                                                                                                                                                                                                              

mtx <- read.table(mtxFn, col.names=c("x", "y", "level"))                                                                                                                                                                                      
mtx$level[(mtx$level == 1)] <- NA                                                                                                                                                                                                             

library(lattice)                                                                                                                                                                                                                              
trellis.device(dev=pdf, file=pdfFn)                                                                                                                                                                                                           
colors <- colorRampPalette(c('red', 'white'))(256)                                                                                                                                                                                            
fig <- levelplot(level~x*y,                                                                                                                                                                                                                   
                 data=mtx,                                                                                                                                                                                                                    
                 col.regions=colors,                                                                                                                                                                                                          
                 xlab="",                                                                                                                                                                                                                     
                 ylab="",                                                                                                                                                                                                                     
                 aspect="iso",                                                                                                                                                                                                                
                 scales=list(                                                                                                                                                                                                                 
                   x=list(rot=90)                                                                                                                                                                                                             
                   ),                                                                                                                                                                                                                         
                 panel=function(...) {                                                                                                                                                                                                        
                   arg <- list(...)                                                                                                                                                                                                           
                   panel.levelplot(...)                                                                                                                                                                                                       
                   panel.text(mtx$x, mtx$y, round(mtx$level*100,0), cex=0.5)                                                                                                                                                                  
                 }                                                                                                                                                                                                                            
                 )                                                                                                                                                                                                                            
print(fig)                                                                                                                                                                                                                                    
graphics.off();

This works fine. I get the following figure:

levelplot

However, instead of having cells labeled NA, I would like to leave them as 1.00 values, but color all cells between 10 (a level of 0.10) and 79 (a level of 0.79) with colors. Anything greater than 79 gets colored with the same color as what is applied to a cell with the approx. level of 79. Or, preferably, said cells would be colored black, with no text inside them at all.

Is there a way to accomplish this with levelplot and lattice?


FINAL EDIT

This doesn't give much of a gradient in colors, but I'm close enough that I'll award the bounty, and perhaps look into ggplot2 as an alternative. Thanks for all your hard work on this.

Here is the final edit of my script:

#! /foo/bar/bin/Rscript --vanilla
args <- commandArgs(TRUE)
dfFn <- args[1]
pdfFn <- args[2]

df <- read.table(dfFn, 
                 col.names=c("x", "y", "level"), 
                 stringsAsFactors=TRUE, 
                 colClasses=c("factor", "factor", "numeric"))
df$level <- round(df$level*100, 0)

# reorder cell type row-factors (in reverse of given order)
df$y <- factor(df$y, levels=unique(df$y[length(df$y):1]))

lowestValue <- min(df$level)
secondHighestValue <- unique(sort(df$level, decreasing=TRUE))[2]

n <- 10
col.seq <- seq(lowestValue, secondHighestValue, length.out=n)
brks <- c(0, col.seq, Inf)
cuts <- cut(df$level, breaks = brks)
colors <- colorRampPalette(c("white", "red"))(length(levels(cuts))-1)
colors <- c(colors, "black")

cls <- rep(colors, times = table(cuts))

library(lattice)
trellis.device(dev=pdf, file=pdfFn)
fig <- levelplot(cuts~x*y,
                 data = df,
                 cuts = n,
                 col.regions=cls,
                 xlab="",
                 ylab="",
                 aspect="iso",
                 scales=list(
                   x=list(rot=90)
                   ),
                 panel=function(...) {
                   arg <- list(...)
                   panel.levelplot(...)
                   panel.text(df$x, df$y, df$level, cex=0.5)
                 },
                 colorkey=list(col=colorRampPalette(c("white", "red"))(length(col.seq)), at=col.seq)
                 )
print(fig)
graphics.off()

Here is the levelplot that this script makes:

vFinal

If I increase n above 15, the cell coloring breaks once again, returning to a diagonal of bright red, instead of black (as shown).

7
  • I would construct bins using cut(x, breaks = c(0, 1, seq(10, 79, 1), 80, Inf)) and assign an appropriate color to each using your colorRampPalette. Through seq, you can always make a desirable number of levels. As for your NA-s, leave out your 5th statement. Dec 21, 2011 at 9:23
  • Please see my revised answer. Dec 22, 2011 at 18:57
  • Can you post a sample of your data online (dropbox or somesuch)? Dec 23, 2011 at 13:12
  • Thanks very much -- please see: dl.dropbox.com/u/31495717/stackoverflow.overlaps.list.txt Dec 24, 2011 at 1:29
  • Ok, another revision. I hope this time you like what you see. Fingers crossed. :) Dec 24, 2011 at 20:15

1 Answer 1

5
+250

This is revision #3

Here we go (again). :)

This is weird, if I set n to anything below 15, things seem to work?

enter image description here

df <- read.table("http://dl.dropbox.com/u/31495717/stackoverflow.overlaps.list.txt",
        sep = "\t", header = FALSE)
names(df) <- c("x", "y", "level")
df$level <- round(df$level*100, 0)

n <- 10
col.seq <- seq(10, 80, length.out = n)
brks <- c(0, seq(10, 80, length.out = n), 100)
cuts <- cut(df$level, breaks = brks)
colors <- colorRampPalette(c("red", "white"))(length(levels(cuts))-1)
colors <- c(colors, "black")

cls <- rep(colors, times = table(cuts))

print(levelplot(cuts~x*y,
                data = df,
                cuts = n,
                col.regions=cls,
                xlab="",
                ylab="",
                aspect="iso",
                scales=list(
                        x=list(rot=90)
                ),
                panel=function(...) {
                    arg <- list(...)
                    panel.levelplot(...)
                    panel.text(df$x, df$y, df$level, cex=0.5)
                },
                colorkey = list(col = colors, at = brks)
        ))
10
  • I apologize, but I do not understand this answer. Dec 21, 2011 at 10:08
  • This is the code you should run (with minor changes to make it fit your bill) before calling levelplot. I suggest you run R interactively to round the edges and then go into "production mode" through a bash script. Dec 21, 2011 at 11:24
  • I do not know how to fit your code into what I have already written, sorry. Where does kats get used, why is it called that, etc.? Dec 21, 2011 at 18:38
  • Where does cls come from, etc.? It is unclear how this answer relates to my particular situation. Dec 21, 2011 at 18:40
  • Sorry, cls was from my previous ("offline") code, then I adapted it for your code. cls is actually kats. I've mended my answer. You put this code before your colors variable (it actually replaces your colors variable). Dec 22, 2011 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.