Skip to content

Arrangement of multiple legends inside legend box #6014

Closed
@clauswilke

Description

@clauswilke

Is there an option to control how multiple legends are laid out inside the legend box (horizontal vs vertical)? I wasn't able to find one, but maybe I'm just missing something. The layout direction seems to change based on where the legend is placed. I would like the legend to look like when I place the legend on top of the plot but place it inside, with individual legends laid out vertically but the legends arranged horizontally next to each other. Is this possible?

library(ggplot2)

ggplot(mtcars) +
  aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt) + 
  geom_point() +
  theme(
    legend.position = "top",
    legend.direction = "vertical"
  )

ggplot(mtcars) +
  aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt) + 
  geom_point() +
  theme(
    legend.position = "inside",
    legend.direction = "vertical"
  )

ggplot(mtcars) +
  aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt) + 
  geom_point() +
  theme(
    legend.position = "inside",
    legend.direction = "horizontal"
  )

Created on 2024-07-26 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions