ggplot图例垂直和水平

问题描述 投票:0回答:1
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")

给我

enter image description here

我如何以一种方式生成图例,即圆柱标签保持垂直,而hp类别则水平排列?

ggplot2 alignment legend direction
1个回答
1
投票

您可以通过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

© www.soinside.com 2019 - 2024. All rights reserved.