Let say I have below ggplot
to draw a stacked bar chart
library(ggplot2)
col_define = c('red', 'orange', 'blue', 'lightblue')
names(col_define) = c('A', 'B', 'C', 'D')
data = rbind(data.frame('grp1' = 'X', 'grp2' = c('A', 'B', 'C', 'D'), 'val' = c(1,2,3,4)), data.frame('grp1' = 'Y', 'grp2' = c('A', 'B', 'C', 'D'), 'val' = c(1,2,3,4)+2))
ggplot(data, aes(x = grp1, fill = grp2, y = val)) +
geom_bar(stat = 'identity', position = 'stack') +
scale_fill_manual(aesthetics = "fill", values = col_define,
breaks = names(col_define))
It is placing all colour in a single legend. However in my case, I basically have 2 groups of colour i.e. one for A & B
and second for C & D
I was looking into a similar discussion in ggplot2: Divide Legend into Two Columns, Each with Its Own Title, where there is an approach to group colours of legend using package ggnewscale
or relayer
However it looks like, this approach can only be applied in ordinary bar chart
, where geom_bar
can be called multiple times.
On the contrary, geom_bar
, in my case, can't be called multiple times, as it is an whole object
I am looking for some way to use ggnewscale
or relayer
package in my stack bar chart
to group colours in the legend.
As @stefan suggested in one of the answers, a possible way to use geom_col
.
However I found that this approach is fairly restrictive, as I cant apply this method for alluvial
plot with the same data as below
library(ggalluvial)
ggplot(data,
aes(x = grp1, stratum = grp2, alluvium = grp2,
y = val,
fill = grp2)) +
geom_flow(aes(fill = grp2), alpha = .3) +
geom_stratum(aes(color = grp2), alpha = .9) +
scale_fill_manual(values = col_define, breaks = names(col_define))
Is there a more general approach to group colours in legend?