1

I want to plot a geom_area of Gi, function of datetime. I want that the fill of each the geom_area for each day is defined by the mean of Gi.

df %>% mutate(year = year(datetime)) %>% 
    filter(month(datetime) == 7) %>% 
    ggplot(aes(datetime, Gi, fill = mean(Gi), group = factor(year))) + 
    geom_area() + facet_wrap("year", scales = "free_x", ncol = 1)

Unfortunately, it's the result, the fill color doesn't vary by years : enter image description here

Rodolphe LAMPE
  • 1,346
  • 2
  • 11
  • 17
  • 2
    You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can see what's going on. – MrFlick Mar 07 '17 at 15:44
  • Do you have a variable that is the mean of GI for each day in your dataset? Sounds like that is the variable you'd want to calculate and map to `fill`. – aosmith Mar 07 '17 at 15:45
  • hehe. `mean(GI)` returns a single number. use the mean by day, not the overall mean like you do now. – Joris Meys Mar 07 '17 at 20:54

1 Answers1

0

when you use fill=mean(Gi), you are mapping the fill color to a single value, the overall mean of Gi!

If I understand correct, what you are trying to do is to color EACH YEAR series with a different color, because they have different means.

In that case, you want something like the below, in dplyr, to build the yearly means:

df %>% 
  group_by(YEAR)
  summarise(YearMean = mean(Gi))
Dan
  • 1,711
  • 2
  • 24
  • 39