Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 478
Philipp Staudacher Avatar asked May 09 '26 11:05

Philipp Staudacher


1 Answers

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

like image 114
chemdork123 Avatar answered May 11 '26 00:05

chemdork123