今天早上,我开始寻找一种解决方案,从我这里的成对值表中生成热图。我发现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")))
-
这几乎创建了我想要的内容
喜欢实现。但灰色的是
丑陋的。我该如何将其更改为,比如说,
蓝色的?用灰色代替蓝色
不起作用...
-
此外,它还可以将标签打印到
按照我在我的中指定的方式勾选
输入表。这适用于
y 轴,但不是 x 轴。怎么可以
我回收 y 轴刻度来打印
它们到 x 轴刻度(旋转
90 度)?
-
最后还有一点点
沿 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")))
-
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...
-
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)?
-
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.
发布评论
评论(2)
我摆弄了相当多的时间,发现刻度标签和矩阵定义现在工作得很好,因为我设法将 row.names 包含到矩阵中。然而,在 levelplot 函数中使用
col.regions
的方式令人大开眼界!我现在使用以下方法在 levelplot 中获取颜色渐变:这将创建白色和蓝色之间的渐变,但任何其他颜色都是可能的。同时确定斜坡的方向。在这里,低值是白色,高值是蓝色。在 levelplot 中,我将此渐变与 col.regions 一起使用,如下所示:
其中长度应设置为任何大值,以便它大于矩阵中不同值的数量。否则颜色会在热图中回收。设置 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 whichcol.regions
can be used in the levelplot function was an eye opener! I now use the following to get a colour ramp inlevelplot
: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 withcol.regions
like this: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!
目前,
matrix
对象是一个字符矩阵,我希望levelplot能够更好地与数字矩阵配合使用,所以我做了:1)你应该意识到
grey
是一个功能。其他颜色函数(例如灰色)包括 rgb 和 hsv。也许..., col.regions=rgb(100) )
...假设您有 101 个不同的 x 值。编辑:那是错误的。 rgb 函数关注三个向量参数:试试这个:2) 轴刻度标签由 scales 参数的列表参数控制。类似于...
,scales=list(x=list(labels=
。这将是与用于设置 z 分隔符的不同的at
。3)我猜测您是由于尺寸或额外值的某些不匹配而沿 x 轴创建了空白。默认水平图不会创建空白。需要您的对象的这些详细信息。事实证明,当暗名是“字符”时,空格存在,但当它们是 NULL 时,空格不存在,所以试试这个:
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:1) You should realize that
grey
is a function. Other color functions likegrey
includergb
andhsv
. 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: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 differentat
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: