Philipp Staudacher
Philipp Staudacher

Reputation: 175

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?

Upvotes: 3

Views: 3233

Answers (1)

chemdork123
chemdork123

Reputation: 13833

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

Upvotes: 4

Related Questions