str(data)
'data.frame': 2425838 obs. of 3 variables:
$ ID : int 10281466 10315034 11392679 12297599 20009616 110540620 114803146 115398695 120231006 130626270 ...
$ factor1: chr "<U+0630><U+0643><U+0631>""| __truncated__ "<U+0630><U+0643><U+0631>""| __truncated__ "<U+0630><U+0643><U+0631>""| __truncated__ "<U+0630><U+0643><U+0631>""| __truncated__ ...
$ months : Date, format: "2015-07-01" "2015-07-01" "2015-07-01" ...
- x轴以月为单位(每个月为一个勾号)。
- y轴为每月绘制的id数。
- 图表应该是一个堆叠面积图(类似于excel中的堆叠面积图)(它是堆叠的,因为我想显示2个堆叠面积图,每个因素一个。
我的问题是我有许多重复的日期值。如何将它们聚集在一起,得到ID_count
,同时保持因子分布。
我尝试的是:
ggplot(data) + geom_area(aes(x = as.Date(factor(months)), y=ID, stat='bin',
fill = factor1, color=factor1), position = 'stack') +
scale_x_date(breaks = "1 month", labels=date_format("%b,%y")) +
theme(axis.text.x=element_text(angle=-90, vjust = 0))
x = as.Date(factor(months)
:尝试使用factor(months)
在x轴上首先放置月份,但需要使用scale_x_date
进一步调整,这就是为什么i
与as.Date()
包裹在一起失败的原因。
这是你想要的吗?
d <- data.frame(day=as.Date('2015-09-16')+sample(-100:100, 500, r=T),
id=sample(1:30, 500, r=T),
factor=sample(c('a','b'), 500, r=T))
d <- aggregate(id ~ format(day, '%Y-%m') + factor, data=d, FUN=length)
colnames(d) <- c('month','factor','id_count')
ggplot(d, aes(x=month,y=id_count,group=factor,fill=factor))+ geom_area(position="stack")