简体   繁体   中英

How do I control a heatmap with lattice and levelplot?

This morning I started to find a solution to produce a heatmap from a table of pair-wise values that I have here. I found that the lattice package offers levelplots which seem like what I was after. When I managed to bring my data into the right format, I played with levelplot . The default colour theme does not produce what I'm after and I found a grey scale solution on the web. However, controlling more of the plot is a bit outside my scope for now. I don't use R very often and am not very familiar yet with many of the deeper data structures.

My current command is:

library("lattice")
matrix <- as.matrix(rev(read.table("inputfile",header=T,row.names=c(<list of ten names>))))
levelplot(matrix,col.regions = grey(100:0/100))

This is the output of dput matrix:

structure(c("937.5", "652.5", "1066.5", "787.5", "229.5", "115.5", 
"787.5", "763.5", "415.5", "N/A", "1483.5", "1360.5", "1858.5", 
"1309.5", "478.5", "322.5", "1375.5", "1588.5", "N/A", "1759.5", 
"643.5", "904.5", "1189.5", "712.5", "256.5", "154.5", "829.5", 
"N/A", "403.5", "1231.5", "808.5", "1096.5", "1195.5", "913.5", 
"292.5", "187.5", "N/A", "1147.5", "478.5", "1192.5", "6712.5", 
"6373.5", "6517.5", "6058.5", "4555.5", "N/A", "6394.5", "7357.5", 
"6214.5", "6835.5", "3412.5", "3991.5", "3814.5", "3793.5", "N/A", 
"1327.5", "3994.5", "3808.5", "3307.5", "4618.5", "1261.5", "1261.5", 
"1162.5", "N/A", "310.5", "220.5", "1195.5", "1639.5", "574.5", 
"1732.5", "871.5", "847.5", "N/A", "610.5", "262.5", "172.5", 
"943.5", "985.5", "448.5", "1525.5", "940.5", "N/A", "1105.5", 
"829.5", "292.5", "160.5", "961.5", "1198.5", "538.5", "1495.5", 
"N/A", "832.5", "1009.5", "880.5", "280.5", "196.5", "952.5", 
"1357.5", "550.5", "1456.5"), .Dim = c(10L, 10L), .Dimnames = list(
    c("NA_Pacific", "NA_Central", "NA_Atlantic", "Greenland", 
    "EU_NW", "EU_WM", "EU_BS.EM", "Asia_SW", "Asia_Central", 
    "Asia_East"), c("Asia_East", "Asia_Central", "Asia_SW", "EU_BS.EM", 
    "EU_WM", "EU_NW", "Greenland", "NA_Atlantic", "NA_Central", 
    "NA_Pacific")))
  1. This creates almost what I would like to achieve. But the grey is ugly. How do I change it to, say, blue? Substituting grey for blue does not work...

  2. Further, it prints labels to the ticks as I specified them in my input table. This works for the y-axis, but not the x-axis. How can I recycle the y-axis ticks to print them to the x-axis ticks (rotated by 90 degrees)?

  3. Finally, there is a tiny little bit of white space along the x-axis (on top and bottom). While this is no real problem, it would however be just nice to eliminate it.

I hope at least some of the questions can be resolved. Thanks in advance.

I fiddled around quite a bit and found the tick labels and matrix definitions now work pretty OK, as I managed to include row.names into the matrix. However, the way in which col.regions can be used in the levelplot function was an eye opener! I now use the following to get a colour ramp in levelplot :

ramp <- colorRamp(c("white", "blue"))

This will create the ramp between white and blue, but any other colour is possible. And direction of the ramp is determined at the same time. Here, low values are white, high values blue. In levelplot I use this ramp with col.regions like this:

col.regions=rgb(ramp(seq(0, 1, length = 1000)), max = 255)

Where length should be set to anything large so that it is larger than amount of different values in the matrix. Otherwise colours get recycled in the heatmap. Setting max=255 defines the whole range between the two colours in ramp. Setting it to lower values will not work because it is the lowest acceptable in the way in which I use it. Setting it higher will shift the "darkness" of the ramp upwards which might also be nice some times.

Not a very professional solution maybe but it works pretty well for my case and I can control my colour ramp nicely. Thanks for the pointers!

At the moment that matrix object is a character matrix and I expect that levelplot will work better with a numeric one, so I made:

matrix2 <-apply(matrix, 2, as.numeric)

1) You should realize that grey is a function. Other color functions like grey include rgb and hsv . Perhaps... , col.regions=rgb(100) ) ... assuming you have 101 distinct x-values. EDIT: That was wrong. the rgb function heeds three vector arguments: Try this:

col.regions = c(rgb(50:0/50, 0, 0),rgb(0,0:50/50,0) )  
     # ugly result but shows how to use two color ranges

2) The axis tick labels are controlled with a list argument to the scales parameter. Something along the lines of... , scales=list(x=list(labels=<label=vector>, at=<tick-positions>) ) . This will be a different at than the one used to set the z-breaks.

3) I am guessing that you have created the whitespace along the x-axis by some mismatch of dimensions or extra values. The default levelplot does not create whitespace. Needed those details of your object. Turns out that the whitespace is present when the dimnames are "character" but not there when they are NULL, so try this:

rownames(matrix2) <- NULL
colnames(matrix2) <- NULL  # don't throw away matrix, we need its dimnames
levelplot(matrix2,col.regions = c(rgb(50:0/50, 0, 0),rgb(0,0:50/50,0) ), region=TRUE, 
    scales=list(x=list(rot=90, at=1:10, labels=rownames(matrix)), y=list( at=1:10, 
    labels=rownames(matrix)) ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM