ggplot geom_area中的未填充区域 [英] Unfilled area in ggplot geom_area

查看:91
本文介绍了ggplot geom_area中的未填充区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ggplot2 geom_area 进行绘制.填充由变量设置.由于某些原因,仅填充外部"组.我不知道如何填充内部区域.

似乎会出现相同的问题

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:

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

解决方案

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

这篇关于ggplot geom_area中的未填充区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆