rggplot2alignmentlegenddirection

ggplot legend vertical AND horizontal


ggplot(mtcars, aes(x = mpg,
                   y = wt,
                   size = hp,
                   colour = as.factor(cyl))) +
  geom_point() +
  theme(legend.direction = "vertical",
        legend.box = "horizontal",
        legend.position = "bottom")

gives me

enter image description here

How can I produce the legend in a way, where the cyl-label remains vertical and the hp categories are arranged horizontally?


Solution

  • You can individually control legends via guides(...):

    ggplot(mtcars, aes(x = mpg,
                       y = wt,
                       size = hp,
                       colour = as.factor(cyl))) +
        geom_point() +
        theme(legend.direction = "vertical",
              legend.box = "horizontal",
              legend.position = "bottom") +
        guides(size=guide_legend(direction='horizontal'))
    

    enter image description here