如何使用点阵图和水平图控制热图?

发布于 2024-11-18 07:39:20 字数 2008 浏览 3 评论 0 原文

今天早上,我开始寻找一种解决方案,从我这里的成对值表中生成热图。我发现lattice包提供了levelplots,这看起来就像我所追求的。当我设法将数据转换为正确的格式时,我使用了 levelplot。默认的颜色主题不能产生我想要的效果,我在网上找到了灰度解决方案。然而,控制更多的情节目前有点超出了我的范围。我不经常使用 R,而且对许多更深层次的数据结构还不是很熟悉。

我当前的命令是:

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

这是 dput 矩阵的输出:

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. 这几乎创建了我想要的内容 喜欢实现。但灰色的是 丑陋的。我该如何将其更改为,比如说, 蓝色的?用灰色代替蓝色 不起作用...

  2. 此外,它还可以将标签打印到 按照我在我的中指定的方式勾选 输入表。这适用于 y 轴,但不是 x 轴。怎么可以 我回收 y 轴刻度来打印 它们到 x 轴刻度(旋转 90 度)?

  3. 最后还有一点点 沿 x 轴的一点空白 (在顶部和底部)。虽然这是 没有真正的问题,但它会是 很高兴消除它。

我希望至少可以解决一些问题。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2024-11-25 07:39:20

我摆弄了相当多的时间,发现刻度标签和矩阵定义现在工作得很好,因为我设法将 row.names 包含到矩阵中。然而,在 levelplot 函数中使用 col.regions 的方式令人大开眼界!我现在使用以下方法在 levelplot 中获取颜色渐变:

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

这将创建白色和蓝色之间的渐变,但任何其他颜色都是可能的。同时确定斜坡的方向。在这里,低值是白色,高值是蓝色。在 levelplot 中,我将此渐变与 col.regions 一起使用,如下所示:

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

其中长度应设置为任何大值,以便它大于矩阵中不同值的数量。否则颜色会在热图中回收。设置 max=255 定义渐变中两种颜色之间的整个范围。将其设置为较低的值将不起作用,因为它是我使用它的方式中可接受的最低值。将其设置得更高将使斜坡的“黑暗度”向上移动,这有时也可能很好。

也许不是一个非常专业的解决方案,但它非常适合我的情况,并且我可以很好地控制我的颜色渐变。感谢您的指点!

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!

西瓜 2024-11-25 07:39:20

目前,matrix对象是一个字符矩阵,我希望levelplot能够更好地与数字矩阵配合使用,所以我做了:

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

1)你应该意识到grey是一个功能。其他颜色函数(例如灰色)包括 rgb 和 hsv。也许..., col.regions=rgb(100) ) ...假设您有 101 个不同的 x 值。编辑:那是错误的。 rgb 函数关注三个向量参数:试试这个:

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) 轴刻度标签由 scales 参数的列表参数控制。类似于... ,scales=list(x=list(labels=, at=) )。这将是与用于设置 z 分隔符的不同的 at

3)我猜测您是由于尺寸或额外值的某些不匹配而沿 x 轴创建了空白。默认水平图不会创建空白。需要您的对象的这些详细信息。事实证明,当暗名是“字符”时,空格存在,但当它们是 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)) ))

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)) ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文