具有多行数据的堆叠geom_area

时间:2018-10-09 14:40:50

标签: r ggplot2

我想做一个堆积面积图。我的数据如下:

data <- structure(list(value = c(3.84, 5.21, 51.16, 0, 7.58, 1.81, 6.74, 
28.27, 6.49, 5.9, 6.15, 0.08, 26.23, 23.76, 24.68, 19.97, 2.89, 
2.84, 4.05, 18.24, 46.83, 30.47, 29.17, 0, 0.15, 0.23, 29.18, 
24.24, 38.58, 21.82, 33.85, 27.23, 27.91, 30.03, 75.12, 5.25, 
6.02, 6.1, 8.62, 81.01, 23.97, 27.99, 19.64, 57.61, 22.22, 19.76, 
18.05, 36.94, 29.13, 20.75, 26.07), category = c("C", "C", "A", 
"B", "C", "C", "C", "A", "C", "C", "C", "A", "B", "B", "B", "B", 
"C", "C", "C", "B", "B", "B", "B", "C", "C", "C", "B", "B", "B", 
"B", "B", "B", "B", "B", "A", "B", "B", "B", "B", "A", "C", "C", 
"C", "A", "C", "C", "C", "A", "A", "C", "C"), date = structure(c(17041, 
17041, 17042, 17042, 17042, 17042, 17042, 17043, 17043, 17043, 
17043, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 
17045, 17045, 17045, 17045, 17045, 17045, 17045, 17046, 17046, 
17046, 17046, 17047, 17047, 17047, 17047, 17048, 17048, 17048, 
17048, 17048, 17049, 17049, 17049, 17049, 17050, 17050, 17050, 
17050, 17051, 17051, 17051, 17051), class = "Date")), class = "data.frame", row.names = c(NA, 
-51L))

我尝试了以下

data %>% ggplot() + geom_area(aes(date, value, fill = category), position = 'stack')

但这给了我: enter image description here

我也尝试过添加stat = 'sum',但是这都不起作用。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

当您有这样混乱的数据时,最好自己在ggplot之外进行数据操作。为了获得面积图,您需要每天将多个观测值折叠成1个值,并在没有观测值的日子中将其包括零。您可以使用

data %>% 
  mutate(category=factor(category)) %>% 
  group_by(date, category) %>% 
  summarize_all(sum) %>% 
  tidyr::complete(date, category, fill=list(value=0)) %>% 
  ggplot(aes(date, value, fill=category)) + 
    geom_area(position="stack")

enter image description here