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