R 制作一个堆叠面积图,每组整齐地叠在一起

R 制作一个堆叠面积图,每组整齐地叠在一起,r,ggplot2,R,Ggplot2,数据框在下面。我正在尝试创建一个堆叠面积图,其中每个组一个叠在另一个上 以下是我目前正在抓的救命稻草: example_df %>% group_by(NEW_REG_DATE, CARD_TYPE) %>% summarise(Count = n()) %>% ggplot(aes(x = NEW_REG_DATE, y = Count, fill = CARD_TYPE, group = CARD_TYPE)) + geom_area(alpha = 0.4) +

数据框在下面。我正在尝试创建一个堆叠面积图,其中每个组一个叠在另一个上

以下是我目前正在抓的救命稻草:

example_df %>% group_by(NEW_REG_DATE, CARD_TYPE) %>% summarise(Count = n()) %>%
  ggplot(aes(x = NEW_REG_DATE, y = Count, fill = CARD_TYPE, group = CARD_TYPE)) +
  geom_area(alpha = 0.4) +
  stat_summary(aes(group = 2), fun.y = sum, geom = 'line', size = 2, alpha = 0.5) +
  scale_x_date(date_labels = "%d-%b", date_breaks = "1 week", expand = c(0, 0)) +
  theme(axis.text.x=element_text(angle=90,hjust=1), legend.position = "bottom")
下面是它的外观:

请注意,这些组看起来是重叠的,很难区分每个组。我希望它们整齐地叠在一起。我该怎么做

以下是数据:

example_df <- structure(list(NEW_REG_DATE = structure(c(16806, 16792, 16792, 
16802, 16826, 16882, 16779, 16783, 16876, 16779, 16822, 16835, 
16802, 16815, 16823, 16867, 16801, 16872, 16800, 16823, 16868, 
16792, 16779, 16853, 16773, 16877, 16820, 16866, 16783, 16779, 
16835, 16832, 16847, 16870, 16872, 16847, 16862, 16854, 16792, 
16893, 16781, 16812, 16816, 16851, 16791, 16867, 16771, 16865, 
16785, 16857, 16787, 16813, 16830, 16849, 16785, 16873, 16856, 
16770, 16810, 16822, 16782, 16877, 16841, 16777, 16865, 16841, 
16830, 16827, 16789, 16850, 16768, 16796, 16784, 16785, 16784, 
16831, 16815, 16853, 16845, 16865, 16770, 16878, 16878, 16789, 
16788, 16796, 16791, 16808, 16814, 16853, 16792, 16820, 16785, 
16770, 16847, 16772, 16810, 16864, 16810, 16800, 16822, 16861, 
16830, 16775, 16810, 16822, 16771, 16826, 16773, 16886, 16777, 
16779, 16867, 16866, 16814, 16859, 16775, 16791, 16809, 16808, 
16871, 16774, 16868, 16791, 16792, 16835, 16848, 16787, 16877, 
16783, 16775, 16822, 16788, 16793, 16793, 16790, 16802, 16796, 
16820, 16779, 16810, 16866, 16783, 16842, 16785, 16772, 16821, 
16794, 16779, 16862, 16802, 16877, 16798, 16802, 16787, 16862, 
16877, 16812, 16800, 16787, 16826, 16868), class = "Date"), CARD_TYPE = c("Youth", 
"Youth", "Youth with Parent", "Youth with Parent", "Youth with Parent", 
"Adult", "Youth", "Youth with Parent", "Youth with Parent", "Youth", 
"Youth with Parent", "Youth", "Youth with Parent", "Youth", "Youth with Parent", 
"Youth", "Youth", "Youth", "Youth", "Youth", "Youth", "Youth with Parent", 
"Youth with Parent", "Youth", "Youth", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth", "Youth", "Youth", "Youth", "Youth with Parent", 
"Youth", "Youth with Parent", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth", "Adult", "Youth with Parent", "Youth with Parent", 
"Youth with Parent", "Youth with Parent", "Youth", "Youth", "Youth", 
"Youth", "Youth", "Youth with Parent", "Youth with Parent", "Youth with Parent", 
"Youth with Parent", "Youth", "Youth with Parent", "Youth", "Youth with Parent", 
"Youth with Parent", "Youth", "Youth", "Youth", "Youth", "Youth with Parent", 
"Adult", "Youth with Parent", "Youth with Parent", "Youth", "Youth with Parent", 
"Youth", "Youth", "Adult", "Youth", "Youth", "Youth with Parent", 
"Youth with Parent", "Youth", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth", "Youth with Parent", "Youth with Parent", "Youth", 
"Youth with Parent", "Youth", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth", "Youth", "Youth", "Youth", "Youth with Parent", 
"Youth", "Youth", "Youth", "Youth", "Youth", "Youth", "Youth", 
"Youth", "Youth with Parent", "Youth", "Youth", "Youth", "Youth", 
"Youth", "Youth", "Youth with Parent", "Youth with Parent", "Youth", 
"Youth", "Youth with Parent", "Youth with Parent", "Youth", "Youth", 
"Youth", "Youth", "Youth", "Youth with Parent", "Youth", "Youth", 
"Youth with Parent", "Youth", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth", "Youth with Parent", "Youth with Parent", "Youth with Parent", 
"Youth", "Youth with Parent", "Youth", "Youth", "Youth", "Youth with Parent", 
"Youth with Parent", "Youth with Parent", "Youth", "Youth", "Youth", 
"Youth", "Youth", "Youth", "Youth with Parent", "Adult", "Youth", 
"Youth with Parent", "Youth", "Youth", "Youth", "Youth", "Youth", 
"Youth", "Youth", "Youth with Parent", "Youth", "Youth with Parent", 
"Youth with Parent", "Youth", "Youth with Parent")), class = "data.frame", row.names = c(NA, 
-162L), .Names = c("NEW_REG_DATE", "CARD_TYPE"))

example\u df您的代码有望正常工作,但问题显然是数据中缺少大量的
(新的\u REG\u日期,卡片类型)
组合。下面我使用
tidyr
添加那些缺少的组合,其中
Count
为零:

library(tidyr)
example_df$CARD_TYPE <- factor(example_df$CARD_TYPE)
example_df %>% group_by(NEW_REG_DATE, CARD_TYPE) %>% summarise(Count = n()) %>%
  complete(NEW_REG_DATE, CARD_TYPE, fill = list(Count = 0)) %>%
  ggplot(aes(x = NEW_REG_DATE, y = Count)) +
  geom_area(aes(fill = CARD_TYPE), alpha = 0.4) +
  stat_summary(aes(group = 2), fun.y = sum, geom = 'line', size = 2, alpha = 0.5) +
  scale_x_date(date_labels = "%d-%b", date_breaks = "1 week", expand = c(0, 0)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), legend.position = "bottom")
library(tidyr)
示例\u df$CARD\u TYPE%group\u by(新注册日期,卡片类型)%%>%summary(Count=n())%%>%
完成(新登记日期、卡片类型、填写=列表(计数=0))%>%
ggplot(aes(x=新登记日期,y=计数))+
几何面积(aes(填充=卡片类型),α=0.4)+
统计汇总(aes(组=2),fun.y=sum,geom=line,size=2,alpha=0.5)+
缩放日期(日期标签=“%d-%b”,日期间隔=“1周”,扩展=c(0,0))+
主题(axis.text.x=element\u text(角度=90,hjust=1),legend.position=“底部”)

Hm,实际上我收到了一条错误消息“f中的错误(…):美学不能随功能区而变化”。有什么想法吗?你在运行我答案中的代码吗?如果不是的话,很难说,听起来有太多的美学问题。在aes中添加group=CARD_类型为我解决了这个问题。非常感谢你的回答我真的在用这个旋转我的轮子我只是好奇,为什么我们需要转换成因子?我的意思是它能工作。。。但你不明白吗?“因子”是r的一部分,它似乎永远不会与meIt一起单击,它代表的是
complete
。如果不收敛到因子,它什么也做不了。我也觉得有点奇怪,但是《完整》
提到了这一点。打印部件不需要这样做。