Using fill and group aesthetics with geom_area

Hi,

It is possible to use the group and fill aesthetics with stack bars with geom_bar. I would like to achieve some similar with geom_area as I need to stack data using a continuous x axis rather than a categorical x-axis... I am having issues finding the proper way to do so, as group seems to somehow overtake the fill aesthetics in geom_area.

(Before you ask: Yes, I really need to display data of different groups with the same fill color :smiley: )

Any help would be greatly appreciated...

data <- bind_rows(
  data.frame(
    time =  as.numeric( rep(seq(1,7), each = 7) ),
    value = runif(49, 10, 100),
    grp = rep(LETTERS[1:7], times = 7),
    task = 'a'
  ),
  data.frame(
    time =  as.numeric( rep(seq(1,7), each = 7) ),
    value = runif(49, 10, 100),
    grp = rep(LETTERS[1:7], times = 7),
    task = 'b'
  )
)

# stacked area chart

ggplot(data, aes(x = time, y = value, fill = grp, group=task)) + 
  geom_bar( stat = "identity", color = 'white' ) 
ggplot(data, aes(x = time, y = value, fill = grp, group=task)) + 
  geom_area( color = 'white' ) 

The first ggplot call below does what you want, I think. I find the second one, using facet_wrap, easier to read, but you may disagree. For some reason I can't get a reprex to work at the moment, so I'm posting only the code.

library(tidyverse)
data <- bind_rows(
  data.frame(
    time =  as.numeric( rep(seq(1,7), each = 7) ),
    value = runif(49, 10, 100),
    grp = rep(LETTERS[1:7], times = 7),
    task = 'a'
  ),
  data.frame(
    time =  as.numeric( rep(seq(1,7), each = 7) ),
    value = runif(49, 10, 100),
    grp = rep(LETTERS[1:7], times = 7),
    task = 'b'
  )
)

# stacked area chart

#ggplot(data, aes(x = time, y = value, fill = grp, group=task)) + 
#  geom_bar( stat = "identity", color = 'white' ) 
ggplot(data, aes(x = time, y = value, fill = grp, group=interaction(task, grp))) + 
  geom_area( color = 'white')

ggplot(data, aes(x = time, y = value, fill = grp, group=interaction(task, grp))) + 
  geom_area( color = 'white') + facet_wrap(~grp)

Thank you FJCC for your response.

However, this does not really address my goal of stacking all group- and fill-related data subsets in one geom_area graph (like for the geom_bar plot)

Sorry, I don't understand how the output of

ggplot(data, aes(x = time, y = value, fill = grp, group=interaction(task, grp))) + 
  geom_area( color = 'white')

is not "stacking all group- and fill-related data subsets in one geom_area graph".

Sorry, I did not see "group=interaction(task, grp) "

Yep, the interaction is doing what I need !

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.