简体   繁体   中英

Align legend horizontal ggplot2

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

在此处输入图像描述

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'))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM