2

I want to use facet_wrap in a way that it allows every subplot have their own legend depending on the e.g. max and min values within each subplot.

When I do:

library(ggplot2)

ggplot(mtcars, aes(cyl, mpg, col = hp)) +
        geom_point() +
        scale_color_viridis_c() +
        facet_wrap(. ~ hp)

I get:

enter image description here

But, the problem here is that I get a common legend, which I do not want. How can I use facet_wrap (or any other technique that I may not know) to make subplots have their own legend?

bird
  • 2,938
  • 1
  • 6
  • 27
  • 6
    I don't think you can have separate legends with faceting, it's often considered one of faceting's "strengths" (efficiencies). You should be able to do something similar with the `patchwork` package, albeit with a little more control on your part. – r2evans Dec 15 '21 at 22:13
  • 5
    See [here](https://stackoverflow.com/questions/14840542/place-a-legend-for-each-facet-wrap-grid-in-ggplot2) – Andre Wildberg Dec 15 '21 at 22:13
  • 3
    Does this answer your question? [Place a legend for each facet\_wrap grid in ggplot2](https://stackoverflow.com/questions/14840542/place-a-legend-for-each-facet-wrap-grid-in-ggplot2) – tjebo Dec 16 '21 at 10:16

0 Answers0