[英]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()
測試數據可以如下生成:
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))
這是我到目前為止最好的:)我也試過玩scale_x / y_date()
我會很高興看到一些讓我走向正確方向的暗示。 謝謝!
你正在尋找這樣的東西嗎?
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
也許嘗試這個:
p_heat <- ggplot(data=data, aes(x=timestamp$mday ,y=timestamp$min+timestamp$hour*60))
+ stat_density2d(geom="tile", aes(fill = value), contour = FALSE)
以下是相關文檔: http : //docs.ggplot2.org/current/stat_density2d.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.