2

I am curious as to whether there is a way to stack legends horizontally rather than vertically in ggplot2 and ggnewscale:

Example using mtcars dataset:

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point(aes(col = gear)) +
  ggnewscale::new_scale_color() +
  geom_point(aes(col = carb))

Plot for example

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Maybe this is useful? https://stackoverflow.com/questions/10032513/ggplot2-legend-to-bottom-and-horizontal –  Dec 03 '21 at 10:17

1 Answers1

2

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

library(ggnewscale)

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point(aes(col = gear)) +
  ggnewscale::new_scale_color() +
  geom_point(aes(col = carb))+
  theme(legend.direction = "vertical",
      legend.box = "horizontal",
      legend.position = "right") +
  guides(size=guide_legend(direction='horizontal'))

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21