时间序列直方图 [英] Time-series histogram
问题描述
可以创建一个时间序列直方图,如这个演示文稿(幻灯片36-39)使用R或D3.js?或者有更好的方法来显示桶数据作为时间序列?
编辑:这里有一些预先存储的示例数据。理想情况下,D3或R会自己做块。是的,如果不清楚,我明白我可以自己写这个。我只是想知道是否已经有一个包这样做,我只是没有碰到它。谢谢!
这里有一个可能的解决方案使用R和ggplot2。
您的数据已准备好粘贴到R控制台中:
dat =结构(列表(日期=结构(c(15541,15541,15541,155441,
15541,155441,155441,155441,155441,155441,155441,155441,155441,
15541,155441,15541 ,15541,15542,15542,15542,15542,155442,
15542,15542,15542,15542,15542,15542,155442,15542,15542,
15542,155443,155443,155443,155443,15543 ,15543,15543,15543,
15543,15543,15543,15543,15543,15543,15543,15543,15543,
15543,15543,155444,155444,155444,155444,155444,155444,155444 ,
15544,15544,15544,15544,15544,15544,15544,15544,15544,
15544,15544,15544,15544,15544,15545,15545,15545,15545,
15545 ,15545,15545,15545,15545,15545,15545,15545,15545,
15545,15545,15545,15545,15546,15546,15546,15546,15546,
15546,15546,15546,15546 ,15546,15546,15546,15546,15546,
15546,15546,15546,15547,15547,15547,15547,15547,15547,
15547,15547,15547,15547,15547,15547,15547 ,15547,15547,
15547,15547,15547,15547),class =Date),bucket = c(800L,
900L,1000L,1100L,1200L,1300L,1400L,1500L,1600L ,1700L,
1800L,1900L,2000L,2100L,2200L,2300L,2400L,800L,900L,
1000L,1100L,1200L,1300L,1400L,1500L,1600L,1700L,1800L,
1900L,2000L,2100L,2200L,900L,1000L,1100L,1200L,1300L,
1400L,1500L,1600L,1700L,1800L,1900L,2000L,2100L,2200L,
2300L,2400L,2500L ,2600L,2800L,800L,900L,1000L,1100L,
1200L,1300L,1400L,1500L,1600L,1700L,1800L,1900L,2000L,
2100L,2200L,2300L,2400L,2500L,2600L ,2700L,2800L,800L,
900L,1000L,1100L,1200L,1300L,1400L,1500L,1600L,1700L,
1800L,1900L,2000L,2100L,2200L,2300L,2400L,800L,900L ,
1000L,1100L,1200L,1300L,1400L,1500L,1600L,1700L,1800L,
1900L,2000L,2100L,2200L,2300L,2400L,1300L,1400L,1500L,
1700L,2200L,2200L,2300L,2400L,
2500L,2600L,2700L,2800L,2900L,3000L,3200L),cnt = c(119L,
123L,173L, 226L,284L,257L,268L,244L,191L,204L,187L,177L,
164L,125L,140L,109L,103L,123L,165L,237L,278L,338L,306L,
316L, 269L,271L,241L,188L,174L,158L,153L,132L,154L,241L,
246L,300L,305L,301L,292L,253L,251L,214L,189L,179L,159L,
161L,144L,139L,132L,136L,105L,120L,156L,209L,267L,299L,
316L,318L,307L,295L,273L,283L,229L,192L,193L,170L,164L,
154L,138L,101L,115L,103L,105L,156L,220L,255L,308L,338L,
318L,255L,278L,260L,235L,230L,185L,145L,147L,157L,109L,
104L,191L,201L,238L,223L,229L,286L,256L,240L,233L,202L,
180L,184L,161L,125L,110L,101L,132L,117L,124L,154L, 167L,
137L,169L,175L,168L,188L,137L,173L,164L,167L,115L,116L,
118L,125L,104L)),.Names = c(date bucket,cnt),
class =data.frame,row.names = c(NA,-125L))
绘制代码:
图库(ggplot2)
plot_1 = ggplot(dat,aes(x = date,y = bucket,fill = cnt))+
geom_tile()+
scale_fill_continuous(low =#F7FBFF,high =#2171B5) +
theme_bw()
ggsave(plot_1.png,plot_1,width = 6,height = 4)
情节可能如果在数据中包含零值桶值的行,请更好地查看。然后,您可以将low =#F7FBFF
更改为low =white
。Is it possible to create a time-series histogram like the one described in this presentation (slides 36-39) using either R or D3.js? Or is there a better way to show bucketed data as a time series?
Edit: Here is some pre-bucketed sample data. Ideally, D3 or R would do the bucketing by itself. And yes, if it wasn't clear, I understand that I could write this myself. I'm just wondering if there's already a package that does this and I just haven't come across it yet. Thanks!
解决方案Here is one possible solution using R and ggplot2.
Your data, ready to paste into R console:
dat = structure(list(date = structure(c(15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15541, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15542, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15543, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15544, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15545, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15546, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547, 15547), class = "Date"), bucket = c(800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 2500L, 2600L, 2800L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L, 2000L, 2100L, 2200L, 2300L, 2400L, 2500L, 2600L, 2700L, 2800L, 2900L, 3000L, 3200L), cnt = c(119L, 123L, 173L, 226L, 284L, 257L, 268L, 244L, 191L, 204L, 187L, 177L, 164L, 125L, 140L, 109L, 103L, 123L, 165L, 237L, 278L, 338L, 306L, 316L, 269L, 271L, 241L, 188L, 174L, 158L, 153L, 132L, 154L, 241L, 246L, 300L, 305L, 301L, 292L, 253L, 251L, 214L, 189L, 179L, 159L, 161L, 144L, 139L, 132L, 136L, 105L, 120L, 156L, 209L, 267L, 299L, 316L, 318L, 307L, 295L, 273L, 283L, 229L, 192L, 193L, 170L, 164L, 154L, 138L, 101L, 115L, 103L, 105L, 156L, 220L, 255L, 308L, 338L, 318L, 255L, 278L, 260L, 235L, 230L, 185L, 145L, 147L, 157L, 109L, 104L, 191L, 201L, 238L, 223L, 229L, 286L, 256L, 240L, 233L, 202L, 180L, 184L, 161L, 125L, 110L, 101L, 132L, 117L, 124L, 154L, 167L, 137L, 169L, 175L, 168L, 188L, 137L, 173L, 164L, 167L, 115L, 116L, 118L, 125L, 104L)), .Names = c("date", "bucket", "cnt"), class = "data.frame", row.names = c(NA, -125L))
Plotting code:
library(ggplot2) plot_1 = ggplot(dat, aes(x=date, y=bucket, fill=cnt)) + geom_tile() + scale_fill_continuous(low="#F7FBFF", high="#2171B5") + theme_bw() ggsave("plot_1.png", plot_1, width=6, height=4)
The plot might look better if you include rows for zero bucket values in your data. Then you could change
low="#F7FBFF"
tolow="white"
.这篇关于时间序列直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!