How to align legend elements in the middle when they are on two rows using ggplot2?

39 views Asked by At

In the MWE below, the caption appears separated into two rows. I want the item corresponding to gear 5 to be aligned in the middle in relation to gears 3 and 4 above it.

How can I do this?

library(ggplot2)

ggplot(data = mtcars,
       aes(x = disp,
           y = qsec,
           shape = as.factor(gear),
           color = as.factor(gear))) +
  geom_line() +
  geom_point() +
  theme(legend.position = "bottom",
        legend.title = element_blank()) +
  guides(shape=guide_legend(nrow = 2,
                            byrow = TRUE),
         color = guide_legend(nrow = 2,
                              byrow = TRUE))

enter image description here

[EDIT]

I want the plot to look like this:

enter image description here

0

There are 0 answers