0

大家好,我想绘制热图:

df ist {xts} 看起来像这样:

structure(c(1.3728813559322, 0.871666666666667, 0.586666666666667, 
0.34, -0.31, -0.973333333333333, -1.52666666666667, -1.71333333333333, 
-0.396666666666667, 0.698333333333333, 2.84666666666667, 4.68333333333333, 
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667, 
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667, 
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", 
"zoo"), index = c(1364770740, 1364774340, 1364777940, 1364781540, 
1364785140, 1364788740, 1364792340, 1364795940, 1364799540, 1364803140, 
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740, 
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340, 
1364849940, 1364853540), .Dim = c(24L, 1L), .Dimnames = list(
NULL, "df.xts"))

在下面的帖子中,我希望 y 轴 24 小时 - 每小时一个值,y 轴是日期。是否可以使用现有的 xts 格式?

ggplot2 热图为中断分配颜色

我还发现了 heatmap.plus() 的另一个例子。

z = matrix(rnorm(30),nrow=5,ncol=6);
rlab = matrix(as.character(c(1:5,2:6,3:7,4:8)),nrow=5,ncol=4);
clab = matrix(as.character(c(1:6,6:1)),nrow=6,ncol=2);
colnames(rlab) = LETTERS[1:dim(rlab)[2]];
colnames(clab) = 1:dim(clab)[2];
heatmap.plus(z,ColSideColors=clab,RowSideColors=rlab);

示例正在运行,但我更喜欢图例,并且我的数据看起来与 df 不同 - 而不是带有日期的 xts。谢谢!

4

1 回答 1

0

这可能不是热图问题的答案,但此代码和输出不会在评论中正确显示。(注意我上面的评论......)使用dat从上面的输出中创建的名为的对象dput,但删除了 tclass-attribute,我将其子集并:

dput(dat['2013-04-01'])
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333, 
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667, 
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667, 
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = c(1364803140, 
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740, 
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340, 
1364849940, 1364853540), .Dim = c(15L, 1L), .Dimnames = list(
    NULL, "df.xts"))
> d2 <- dat['2013-04-01']
> tclass(d2) <- "POSIXct"
> d2   #displays with time appropriate format
                       df.xts
2013-04-01 00:59:00 0.6983333
2013-04-01 01:59:00 2.8466667
2013-04-01 02:59:00 4.6833333
2013-04-01 03:59:00 5.3383333
2013-04-01 04:59:00 5.6666667
2013-04-01 05:59:00 5.6366667
2013-04-01 06:59:00 5.6900000
2013-04-01 07:59:00 5.6966667
2013-04-01 08:59:00 5.5433333
2013-04-01 09:59:00 5.5083333
2013-04-01 10:59:00 4.3350000
2013-04-01 11:59:00 3.0650000
2013-04-01 12:59:00 2.4266667
2013-04-01 13:59:00 1.8866667
2013-04-01 14:59:00 1.4783333
> dput(d2)
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333, 
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667, 
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667, 
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1364803140, 
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740, 
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340, 
1364849940, 1364853540), tclass = c("POSIXct", "POSIXt")), .Dim = c(15L, 
1L), .Dimnames = list(NULL, "df.xts"))

在 : 之后没有添加 tclass 属性tclass(d2) <- "POSIXct"。所以我想知道热图轴问题是否与格式不正确的 xts 对象有关。

于 2013-06-09T20:04:17.190 回答