简体   繁体   English

像莱迪思的情节热图

[英]Heatmap like plot with Lattice

I can not figure out how the lattice levelplot works. 我无法弄清楚晶格levelplot工作原理。 I have played with this now for some time, but could not find reasonable solution. 我已经玩了一段时间,但是找不到合理的解决方案。

Sample data: 样本数据:

Data <- data.frame(x=seq(0,20,1),y=runif(21,0,1))
Data.mat <- data.matrix(Data)

Plot with levelplot: 用水平图绘制:

rgb.palette <- colorRampPalette(c("darkgreen","yellow", "red"), space = "rgb")

levelplot(Data.mat, main="", xlab="Time", ylab="", col.regions=rgb.palette(100),   
          cuts=100, at=seq(0,1,0.1), ylim=c(0,2), scales=list(y=list(at=NULL)))

This is the outcome: 结果如下:

在此处输入图片说明

Since, I do not understand how this levelplot really works, I can not make it work. 由于我不了解该级别图的实际工作原理,因此无法使其正常工作。 What I would like to have is the colour strips to fill the whole window of the corresponding x (Time). 我想要的是用彩色条填充相应x (时间)的整个窗口。

Alternative solution with other method. 其他方法的替代解决方案。

Basically, I'm trying here to plot the increasing risk over time, where the red is the highest risk = 1. I would like to visualize the sequence of possible increase or clustering risk over time. 基本上,我试图在此处绘制随时间增加的风险,其中红色是最高风险=1。我想可视化随时间推移可能增加或聚类的风险的顺序。

Unfortunately I don't know much about lattice , but I noted your "Alternative solution with other method", so may I suggest another possibility: 不幸的是,我对lattice不是很了解,但是我注意到了您的“其他方法的替代解决方案”,所以我建议另一种可能性:

library(plotrix)
color2D.matplot(t(Data[ , 2]), show.legend = TRUE, extremes = c("yellow", "red"))

Heaps of things to do to make it prettier. 使它变得更漂亮需要做大量的事情。 Still, a start. 尽管如此,还是一个开始。 Of course it is important to consider the breaks in your time variable. 当然,考虑时间变量的中断很重要。 In this very simple attempt, regular intervals are implicitly assumed, which happens to be the case in your example. 在这种非常简单的尝试中,隐式假定了常规时间间隔,在您的示例中正是这种情况。

Update Following the advice in the 'Details' section in ?color2D.matplot : "The user will have to adjust the plot device dimensions to get regular squares or hexagons, especially when the matrix is not square". 按照?color2D.matplot “详细信息”部分中的建议进行更新 :“用户将必须调整绘图设备的尺寸以获得规则的正方形或六边形,尤其是在矩阵不是正方形的情况下”。 Well, well, quite ugly solution. 好吧,很好,很丑陋的解决方案。

par(mar = c(5.1, 4.1, 0, 2.1))
windows(width = 10, height = 2.5)
color2D.matplot(t(Data[ , 2]),
                show.legend = TRUE,
                axes = TRUE,
                xlab = "",
                ylab = "",
                extremes = c("yellow", "red"))

在此处输入图片说明

From ?levelplot we're told that if the first argument is a matrix then "'x' provides the 'z' vector described above, while its rows and columns are interpreted as the 'x' and 'y' vectors respectively.", so ?levelplot我们得知,如果第一个参数是矩阵,则“'x'提供上述'z'向量,而其行和列分别被解释为'x'和'y'向量。”,所以

> m = Data.mat[, 2, drop=FALSE]
> dim(m)
[1] 21  1
> levelplot(m)

plots a levelplot with 21 columns and 1 row, where the levels are determined by the values in m. 绘制具有21列和1行的水平图,其中水平由m中的值确定。 The formula interface might look like 公式界面可能看起来像

> df <- data.frame(x=1, y=1:21, z=runif(21))
> levelplot(z ~ y + x, df)

(these approaches do not quite result in the same image). (这些方法不能完全产生相同的图像)。

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

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