ggplot geom_area中的未填充区域

时间:2016-06-23 13:21:36

标签: r ggplot2 fill

我试图用ggplot2geom_area做一个情节。填充由变量设置。出于某种原因,只有'外部'团体充满了。我无法弄清楚如何填充内部区域。

同样的问题似乎发生here,但没有给出如何解决它的答案。

以下是我使用的代码的最小示例和结果图:

and the resulting plot

我使用的是R 3.3和ggplot2_2.1.0

任何帮助将不胜感激。

df <- data.frame(month = seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-31"), by = "month"), 
             type = c(rep("past", times = 5), "current", rep("future", times = 6)), 
             amount = c(seq(from = 100, to = 1200, by = 100)))

df$type <- factor(df$type, levels = c("past", "current", "future"))


ggplot(data = df, aes(x = month, y = amount, fill = type)) + 
  geom_area()

1 个答案:

答案 0 :(得分:0)

我在&#34;当前&#34;中添加了2个时间点。价值以产生一个区域。问题是只有一个点就不能绘制区域。

library(ggplot2)

df <- data.frame(month = seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-31"), by = "month"), 
                 type = c(rep("past", times = 5), "current", rep("future", times = 6)), 
                 amount = c(seq(from = 100, to = 1200, by = 100)))

df <- rbind(df[1:5, ], 
            data.frame(month = as.Date(c("2016-05-15", "2016-06-15")),
                       type  = c("current", "current"),
                       amount = c(550, 650)),
            df[7:12, ])

df$type <- factor(df$type, levels = c("past", "current", "future"))


ggplot(data = df, aes(x = month, y = amount, fill = type)) + 
geom_area()  

enter image description here