R - 堆积面积图

时间:2018-06-02 10:43:30

标签: r data-visualization data-science

对于给定的数据集,我如何在不同的源中生成堆积面积图流入与日期,使得最大总流入源最接近X(日期)轴,最少流入源最远。

Date    Source  Inflow  
1/1/06  s1       271   
1/1/06  s3       368  
1/1/06  s2       425  
1/2/06  s4       580  
1/2/06  s2       233  
1/2/06  s3       243  
1/2/06  s1       428  
1/3/06  s5       164  
1/3/06  s2       461  
1/3/06  s3       180  
1/4/06  s1       258  
1/4/06  s2       153  
1/5/06  s6       443 

ggplot(data, aes(x=Inflow, y=Date, fill=Source)) + geom_area(colour="black", size=.2, alpha=.4) + scale_fill_brewer(palette="Greens", breaks=rev(levels(data$Source)))

Please find the attached Plot

1 个答案:

答案 0 :(得分:3)

老实说,堆积区域图表对您提供的数据没有多大意义,因为某些类别在一天内被发现(因此区域无处扩展,因此垂直线条)。

堆积条形图可能是最佳选择。

library("tidyverse")


records <- tribble(~Date, ~Source, ~Inflow, 
        "1/1/06",  "s1",      271,   
        "1/1/06",  "s3",      368,  
        "1/1/06",  "s2",      425,  
        "1/2/06",  "s4",      580,  
        "1/2/06",  "s2",      233,  
        "1/2/06",  "s3",      243,  
        "1/2/06",  "s1",      428,  
        "1/3/06",  "s5",      164,  
        "1/3/06",  "s2",      461,  
        "1/3/06",  "s3",      180,  
        "1/4/06",  "s1",      258,  
        "1/4/06",  "s2",      153,  
        "1/5/06",  "s6",      443)

records %>% 
  mutate(Date = as.Date(lubridate::mdy(Date))) %>% 
  ggplot(mapping = aes(x=Date, y=Inflow, fill=Source)) +
  geom_area(colour="black", size=.2, alpha=.4, position = position_stack()) +
  scale_fill_brewer(palette="Greens")

records %>%  
  mutate(Date = as.Date(lubridate::mdy(Date))) %>% 
  ggplot(mapping = aes(x=Date, y=Inflow, fill=Source)) +
  geom_col(colour="black", size=.2, alpha=.4, position = position_stack()) +
  scale_fill_brewer(palette="Greens")

相关问题