【问题标题】:ggplot legend vertical AND horizontalggplot图例垂直和水平
【发布时间】:2020-05-04 16:47:42
【问题描述】:
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")

给我

如何以某种方式生成图例,其中 cyl-label 保持垂直而 hp 类别水平排列?

【问题讨论】:

    标签: ggplot2 alignment legend direction


    【解决方案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'))
    

    【讨论】: