简体   繁体   English

如何使用 lattice 和 levelplot 控制热图?

[英]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.我发现格子levelplots提供了看起来像我所追求的水平图。 When I managed to bring my data into the right format, I played with levelplot .当我设法将我的数据转换为正确的格式时,我使用了levelplot The default colour theme does not produce what I'm after and I found a grey scale solution on the web.默认颜色主题不会产生我想要的,我在 web 上找到了灰度解决方案。 However, controlling more of the plot is a bit outside my scope for now.但是,现在控制更多 plot 有点超出我的 scope 范围。 I don't use R very often and am not very familiar yet with many of the deeper data structures.我不经常使用 R 并且对许多更深层次的数据结构还不是很熟悉。

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:这是 dput 矩阵的 output:

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.这适用于 y 轴,但不适用于 x 轴。 How can I recycle the y-axis ticks to print them to the x-axis ticks (rotated by 90 degrees)?如何回收 y 轴刻度以将它们打印到 x 轴刻度(旋转 90 度)?

  3. Finally, there is a tiny little bit of white space along the x-axis (on top and bottom).最后,沿 x 轴(顶部和底部)有一点点空白。 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.我摆弄了很多,发现刻度标签和矩阵定义现在工作得很好,因为我设法将row.names包含到矩阵中。 However, the way in which col.regions can be used in the levelplot function was an eye opener!然而,在col.regions中使用 col.regions 的方式令人大开眼界! I now use the following to get a colour ramp in levelplot :我现在使用以下内容在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:levelplot ,我将这个斜坡与col.regions一起使用,如下所示:

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.设置max=255定义了渐变中两种颜色之间的整个范围。 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:目前, matrix object 是一个字符矩阵,我希望 levelplot 与数字矩阵一起使用会更好,所以我做了:

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

1) You should realize that grey is a function. 1)您应该意识到grey是 function。 Other color functions like grey include rgb and hsv . grey等其他颜色函数包括rgbhsv Perhaps... , col.regions=rgb(100) ) ... assuming you have 101 distinct x-values.也许... , col.regions=rgb(100) ) ... 假设您有 101 个不同的 x 值。 EDIT: That was wrong.编辑:那是错误的。 the rgb function heeds three vector arguments: Try this: rgb function 注意三个向量 arguments:试试这个:

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. 2) 轴刻度标签由 scales 参数的列表参数控制。 Something along the lines of... , scales=list(x=list(labels=<label=vector>, at=<tick-positions>) ) .类似于... , 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.这将不同于用于设置 z-breaks 的at

3) I am guessing that you have created the whitespace along the x-axis by some mismatch of dimensions or extra values. 3)我猜你已经通过尺寸或额外值的一些不匹配创建了沿 x 轴的空白。 The default levelplot does not create whitespace.默认的 levelplot 不会创建空白。 Needed those details of your object.需要您的 object 的这些详细信息。 Turns out that the whitespace is present when the dimnames are "character" but not there when they are NULL, so try this:事实证明,当暗名是“字符”时存在空格,但当它们是 NULL 时不存在,所以试试这个:

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)) ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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