0

我有 2 列的数据框;其中第一列将数据保存为 d/m/yh:m(一年),而第二列保存电表读数。例如:

Date.Time           X
13/12/2014 12:00    164
13/12/2014 12:30    278
13/12/2014 13:00    159
13/12/2014 13:30    302
13/12/2014 14:00    249
13/12/2014 14:30    196
.
.
.
14/12/2014 00:00    137
14/12/2014 00:30    71
14/12/2014 01:00    116
14/12/2014 01:30    92

我想为每一天每半小时绘制一次热图。我试过了pheatmapheatmap.2heatmap.3错误信息是x' must be a numeric matrix。我只是想知道我可以用这两列绘制一张地图,还是应该先将数据排列到一行,呈现一天的数据,例如:

day         12:00 12:30 1:00 1:30
13/12/2014    164  278  159 302 ...
14/12/2014    160  178  129 602 ...

请问有什么建议吗?谢谢!

4

1 回答 1

1

您不想将数据从长格式更改为宽格式;您只需要拆分日期和时间,就可以在不同的轴上使用它们。这是我对一些玩具数据所做的:

library(ggplot2)

# Make some toy data with a year's worth of observations on every half hour
df <- data.frame(date.time = seq(from = as.POSIXct("2015-01-01"), to = as.POSIXct("2015-12-31"), by=30))
df$X <- rnorm(nrow(df), 100, 25)

# Create separate vectors for date and time, preserving date/time format
df$date <- as.Date(df$date.time)
df$time <- strftime(df$date.time, format="%H:%M:%S")

# Now use ggplot2's geom_tile() to make the heatmap
p <- ggplot(df, aes(x=time, y=date, fill=X)) + geom_tile()
print(p)
于 2015-08-20T18:47:30.727 回答