Using ggh4x::facet_grid2
we can colour facet strips by group, see here.
Can the same be done if you are using ggh4x::facet_manual
so that for the graph below each strip on the same row (group_var
e.g. A
, B
, C
will be blue
) will have the same colour?
library(ggplot2)
library(ggh4x)
dat <- data.frame(
facet = LETTERS[1:8],
x = 1,
y = 1,
group_var = c(1, 1, 1,
2, 2,
3, 3, 3)
)
dat
design = "
ABC
D#E
FGH
"
ggplot(dat, aes(x, y)) +
geom_point() +
facet_manual(vars(group_var, facet), design = design,
strip = strip_split(position = c("left", "top")))
So given the solution to ggh4x::facet_grid2, something like this is needed which doesnt produce what I want:
ggplot(dat, aes(x, y)) +
geom_point() +
facet_manual(vars(group_var, facet), design = design,
strip = strip_themed(
strip_split(position = c("left", "top")),
background_x = list(element_rect(fill = "red"),
element_rect(fill = "green"),
element_rect(fill = "blue"))))
thanks
Following ggh4x-strips background_x = list(...)
needs to go inside strip_split
.
?strip_split
-> background_x, background_y
A list() with ?element_rect
elements. See the details section in ?strip_themed
.
background_x
then seems to read fill
in order of your faceted plots.
library(ggplot2)
library(ggh4x)
dat <- data.frame(
facet = LETTERS[1:8],
x = 1,
y = 1,
group_var = c(1, 1, 1,
2, 2,
3, 3, 3)
)
design = "
ABC
D#E
FGH
"
# for building the list with "element_rect"
cols <- c("group1" = "dodgerblue",
"group2" = "red",
"group3" = "green")
ggplot(dat, aes(x, y)) +
geom_point() +
facet_manual(
vars(group_var, facet),
design = design,
strip = strip_split(
position = c("left", "top"),
background_x = lapply(cols[dat$group_var], element_rect) # or elem_list_rect(fill = cols[dat$group_var])
)
)
giving