0

I have following code:

ggplot(data = mpg[is.element(mpg$manufacturer, c("audi", "dodge", "ford")),], mapping = aes(x = year, colour = manufacturer)) +
  geom_line(stat = "count") +
  facet_wrap(~manufacturer, nrow = 3)

This gives me this plot, but what I want is to separate the legend for every grid with a space. The underlying question is if there is a way to manipulate individual legend entries and manipulate their position?

plot

Thanks!

chemdork123
  • 12,369
  • 2
  • 16
  • 32
pgitti
  • 87
  • 7
  • Maybe you can give a try to the `gridExtra` package. See [this post](https://stackoverflow.com/questions/14840542/place-a-legend-for-each-facet-wrap-grid-in-ggplot2) in case you want to use it. – patL Aug 25 '21 at 08:35
  • Can you explain a bit more what you mean "separate legend for every grid with a space?" – daileyco Aug 25 '21 at 15:39
  • You may find this post to be supportive: https://stackoverflow.com/questions/14840542/place-a-legend-for-each-facet-wrap-grid-in-ggplot2 – Susan Switzer Aug 25 '21 at 20:04

1 Answers1

0

There's a few ways to approach this. You can go about using the gridExtra package and basically construct your plot piece by piece (constructing grobs or "graphical objects"). This way should work, but it is kind of cumbersome.

The easier way is to familiarize yourself with all of ggplot2's theme elements that together will give you control over all aspects of your plot.

Here's the elements that I used together inside of theme() to get things to look right:

  • legend.key.height. This element controls the height of each of the legend "keys". These are the symbols that represent the lines next to the title of the key.
  • legend.key.width. Width of legend keys... same deal.
  • legend.key. We set this to element_blank(). It's the background portion of the key. If I did not set this to blank, then you'd have those large gray rectangles underneath the lines and it looks weird. Leave this out and you'll see what I mean.
  • legend.title. This controls the theming of the legend title. Here, I use it to control the margin of the title as you'll see...
  • plot.margin. The area around the plot.

First, let's control the placement of the keys themselves to spread them out a bit vertically. We can do that by setting the height of each key to be about 1/3 of the total space of the plot. "npc" is the unit that basically corresponds to the relative plot area, so 0.33 npc would be a bit less than one third of the plot for the size of each key. I make the keys wider with legend.key.width, and then I remove the gray background for each key with legend.key = element_blank().

plot + theme(
    legend.key.height = unit(0.3, "npc"),
    legend.key.width = unit(30, "pt"),
    legend.key = element_blank()
  )

enter image description here

This gets us close, but not quite there. The reason is that the legend title is still lined up with the top of the plot. Optimally, you want the title above the top of the plot so that the keys line up centered with each plot. In order to do that, we can use a bit o' trickery... I can trick ggplot2 to move the title of the legend up by setting the margin to a negative number! That will move the title up, but will also put it above the plot area. In response, we'll also increase the top margin of our plot area to ensure the title remains on the plot. Here's the final code to do that with the resulting plot:

plot + theme(
    legend.key.height = unit(0.3, "npc"),
    legend.key.width = unit(30, "pt"),
    legend.key = element_blank(),
    legend.title = element_text(margin=margin(t=-30)),
    plot.margin=margin(t=30)
  )

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32