Is it possible to have independent strip themes for each variable used in ggplot2 facet_wrap
?
Take this chunk of code as an example:
p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(c("cyl", "drv"), labeller = "label_both")
plot(p1)
I would want to have upper strip ('cyl') with a different theme - say, in bold. Additionally, I might want to make 'drv' italic and with a different font type and size. How could I do that?
I was thinking something in the lines of:
p1 <- p1 + theme(strip.text.variable1 = element_text(face = 'bold'),
strip.text.variable2 = element_text(face = 'italic', size = 8)
)
Unfortunately, I couldn't find anything like this in the docs or previous questions.
Cheers
Edit: I made the question a bit more general to be of further help to the community.
Ostensibly you should be able to make a new function based on label_both
to return bold labels, but so far my attempts have ended with the dreaded Error in variable[[i]] : subscript out of bounds
.
An alternative to this is to build a function to make the label you want. This is much like this answer. In this function you add the prefix to the values of the variable and make them bold.
make_labels = function(string, prefix = "cyl: ") {
x = paste0(prefix, as.character(string))
do.call(expression, lapply(x, function(y) bquote(bold(.(y)))))
}
Now use this function within as_labeller
for the "cyl" variable in facet_wrap
. You want to change the default labeller within as_labeller
to label_parsed
so the expression is parsed correctly. Use label_both
for the other variable.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(c("cyl", "drv"),
labeller = labeller(cyl = as_labeller(make_labels, default = label_parsed),
drv = label_both))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With