Unfilled area in ggplot geom_area

我是研究僧i 提交于 2021-02-11 04:58:24

问题


I'm trying to do a plot with ggplot2 and geom_area. The fill is set by a variable. For some reason, only the 'outer' groups are filled. I can't figure out how to get the inner regions filled as well.

The same problem seems to occur here but no answer is given on how to solve it.

Below is an minimal example of the code i'm using and the resulting plot:

and the resulting plot

I'm using R 3.3 and ggplot2_2.1.0

Any help would be appreciated.

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:


I added 2 points in time arround the "current" value in order to produce an area. The problem is that with only one point no area can be drawn.

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()  



来源:https://stackoverflow.com/questions/37992906/unfilled-area-in-ggplot-geom-area

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!