ggplot 图例垂直和水平

ggplot legend vertical AND horizontal

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 类别水平排列?

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