绘制时间序列作为热图 [英] Plot timeseries as heatmap
问题描述
我有一个带有时间戳和值的数据框(大约每5分钟一个值)。
现在我想要绘制x轴上的日子,y轴上的日子以及颜色的值。最好的情况是,如果我可以控制y轴上的分档(5分钟,10分钟,15分钟......每个分档的平均值)。
我是试用ggplot2,但我无法得到合理的结果。
p_heat< - ggplot(data = data,
aes(x = days(timestamp),
y = minutes(timestamp)+ hours(timestamp)* 60,fill = value))+
geom_tile()
$ c $测试数据可以生成如下:
<$ c $ c> data< - data.frame(timestamp = seq(from = as.POSIXct(2013-09-01 00:00:00),
to = as.POSIXct(2013-10-01
value = runif(8641,0,1))
这是迄今为止我可以获得的最好效果:)
我也尝试过使用scale_x / y_date()
我会很高兴看到一些将我推向正确方向的暗示。感谢!
这是你想要的东西吗?
< pre $
library(ggplot2)
#为x轴创建日期变量
df $ date< - as.Date(df $ timestamp, (格式=%Y-%m-%d)
#get H:M分量
df $ hm < - format(df $ timestamp,%H:%M )
#创建y轴中断和标签
lab < - with(df,paste(format(df $ timestamp,%H),00,sep = :))
gg < - ggplot(data = df,aes(x = date,y = hm,fill = value))+
geom_tile()+
scale_y_discrete(breaks = lab)
gg
I have a data frame with timestamps and values (a month with values approximately every 5min). Now I want to have plot with the day on the x-axis, the time of day on the y-axis and the value as the color. Best would be if I can control the binning on the y-axis (5min, 10min, 15min.. with the value as the mean for each bin).
I am experimenting with ggplot2, but I can't get a reasonable result.
p_heat <- ggplot(data = data,
aes(x = days(timestamp),
y = minutes(timestamp) + hours(timestamp) * 60, fill = value)) +
geom_tile()
Test data can be generated as follows:
data <- data.frame(timestamp = seq(from = as.POSIXct("2013-09-01 00:00:00"),
to = as.POSIXct("2013-10-01 00:00:00"), by = "5 mins"),
value = runif(8641, 0, 1))
This was the best I could get so far :) I also tried playing with scale_x/y_date()
I would be glad about some hints which push me into the right direction. Thanks!
Is it something like this you are looking for?
library(ggplot2)
# create date variable for the x-axis
df$date <- as.Date(df$timestamp, format = "%Y-%m-%d")
# get H:M components
df$hm <- format(df$timestamp, "%H:%M")
# create y-axis breaks and labels
lab <- with(df, paste(format(df$timestamp, "%H"), "00", sep = ":"))
gg <- ggplot(data = df, aes(x = date, y = hm, fill = value)) +
geom_tile() +
scale_y_discrete(breaks = lab)
gg
这篇关于绘制时间序列作为热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!