'How to specify different background colors for each facet label in ggplot2?

A plot will be made from these data:

  mtcars %>%
  gather(-mpg, key = "var", value = "value") %>%
  ggplot(aes(x = value, y = mpg)) +
  geom_point() +
  facet_wrap(~ var, scales = "free") +
  theme_bw()

How can I change the gray color of the titles of the panels for instance

 panels of am and hp   green 
 panels of gear drat disp   red 
 panels  of vs wt   blue
 panels cyl qsec carb black 

add a legend

      green  = area 
      red=  bat
      blue= vege
      black = indus


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source