首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制作一个堆叠的区域图,每个组都整齐地放在另一个组的上面。

制作一个堆叠的区域图,每个组都整齐地放在另一个组的上面。
EN

Stack Overflow用户
提问于 2018-03-26 09:36:12
回答 1查看 33关注 0票数 1

数据帧在下面。我试图创建一个堆叠的区域图,其中每个组被堆叠,一个在另一个上面。

下面是我目前所掌握的问题:

代码语言:javascript
复制
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")

以下是它的外观:

注意,这些组看起来好像是重叠的,很难区分每个组。我想把它们整齐地叠在一起。我怎么能这么做?

以下是数据:

代码语言:javascript
复制
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"))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-26 12:33:15

您的代码是可以工作的,但是很明显,问题在于数据中缺少大量的(NEW_REG_DATE, CARD_TYPE)组合。下面我使用tidyr添加那些Count为0的缺失组合:

代码语言:javascript
复制
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")

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49488250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档