Currently, I'm using the latest version of R and I have found that this script widely used is outdated and doesn't work. This is the site
Apparently this script worked very well in the previous versions
library(tidyverse)
library(grid)
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)
g <- ggplot_gtable(ggplot_build(p))
stripr <- which(grepl('strip-r', g$layout$name))
fills <- c("red","green","blue","yellow")
k <- 1
for (i in stripr) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)
This produces the following graph:
However, when I run it, I get the error
Error in *tmp* [[j]] : attempt to select less than one element in get1index
Thus, I would like to ask the community if you have found another method to change the colors of each rectangle of the label, since ggplot2 natively doesn't do it.
Thanks