mxs*_*her 2 aggregate r ggplot2
我有一个800k行的数据集,每个都有一个时间戳.数据涵盖一年的时间范围.
在准备我想要制作的堆积区域图时,我想创建26个子组(例如,两周间隔).在这些子组中,我想找到5个类中的值的频率.
例如:在前两周,这些值的百分比是> x && <= y,有多少是> y && <= z等.
所有这些都应该导致ggplot2库及其geom_area()函数创建的堆积区域图.
这是数据集的头部:
date transaction_volume transaction_costs
47 2015-01-01 3.985826 0.03157
59 2015-01-01 3.955749 0.03157
71 2015-01-01 0.315700 0.03157
72 2015-01-01 0.315700 0.03157
73 2015-01-01 0.315700 0.03157
74 2015-01-01 0.315700 0.03157
Run Code Online (Sandbox Code Playgroud)
这是一个虚拟数据的例子:
library(dplyr)
library(ggplot2)
n <- 1000
d <- data.frame(date=as.Date('2010/01/01') + sort(sample(0:364, n, replace=TRUE)))
d$x <- runif(n)
# These are the breaks defining your bins of data
breaks <- c(0, 0.2, 0.4, 0.6, 0.8, 1)
d %>%
# create fortnight indicator from Julian day number
mutate(Fortnight=ceiling(as.numeric(format(date, '%j'))/14)) %>%
# bin data
mutate(Class=factor(findInterval(x, breaks))) %>%
group_by(Fortnight, Class) %>%
# count per group
summarise(n=n()) %>%
# expressed as proportions
mutate(Proportion=n/sum(n)) %>%
ggplot(aes(x=Fortnight, y=Proportion, fill=Class)) +
geom_area()
Run Code Online (Sandbox Code Playgroud)
如果你想稍微清理它,请过滤两周27.例如filter(Fortnight < 27) %>%在ggplot通话前插入.
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |