Let say I have below ggplot
library(ggplot2)
library(ggnewscale)
data =
structure(list(grp1 = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"
), grp2 = c("A", "B", "C", "D", "A", "B", "C", "D"), val = c(1,
2, 3, 4, 3, 4, 5, 6)), row.names = c(NA, -8L), class = "data.frame")
col_define = c('red', 'orange', 'blue', 'lightblue')
names(col_define) = c('A', 'B', 'C', 'D')
ggplot(data, aes(x = grp1, group = grp2, y = val)) +
geom_col(aes(fill = grp2)) +
scale_fill_manual(values = col_define, breaks = c("A", "B"), name = "1") +
new_scale_fill() +
geom_col(aes(fill = grp2)) +
scale_fill_manual(values = col_define, breaks = c("C", "D"), name = "2") +
theme(legend.position="top", legend.direction = 'vertical',
legend.box.margin = margin(),
legend.box.background = element_rect(fill = alpha('#e5e5e5', 0.60), size = 0.1, linetype = 'solid', color = '#333333'))
As we can seen I tried to add background transparency in my plot using element_rect(fill = alpha('#e5e5e5', 0.60)
, however is not working. Is there any alternate way to add transparency in the background for this plot?
I also want to change the order in label of legend like 2, 1
, instead 1, 2
. Basically I want to apply some custom ordering in case there are many levels.
Any pointer will be very helpful.