1

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()
Community
  • 1
  • 1
thoring
  • 13
  • 2
  • The reason is that `current` occurs only once. So you do not have an area. – Alex Jun 23 '16 at 13:25
  • 1
    It is probably better to use `geom_bar(stat = 'identity')` – Jaap Jun 23 '16 at 13:30
  • `ggplot(data = df, aes(x = month, y = amount, fill = type)) + geom_bar(stat = "identity") ` – Alex Jun 23 '16 at 13:32
  • Of course it makes complete sense that an area could not be drawn with one data point. Doh! I'm going to stick with the geom_area because the real plot is supposed to show the gradual increase of the variable. I'm going to mark the 'current' period with a vertical line. Thanks ! – thoring Jun 23 '16 at 13:54

1 Answers1

0

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

enter image description here

Alex
  • 4,925
  • 2
  • 32
  • 48
  • thanks for your answer Alex. This does indeed solve the fill issue. But after reading your answer and the comments above i've decided to go another route (see comment above). – thoring Jun 23 '16 at 13:59