当图例元素位于两行时,如何使用ggplot2将图例元素居中对齐?

问题描述 投票:0回答:1

在下面的 MWE 中,标题显示为分为两行。我希望与齿轮 5 相对应的项目相对于其上方的齿轮 3 和齿轮 4 在中间对齐。

我该怎么做?

library(ggplot2)

ggplot(data = mtcars,
       aes(x = disp,
           y = qsec,
           shape = as.factor(gear),
           color = as.factor(gear))) +
  geom_line() +
  geom_point() +
  theme(legend.position = "bottom",
        legend.title = element_blank()) +
  guides(shape=guide_legend(nrow = 2,
                            byrow = TRUE),
         color = guide_legend(nrow = 2,
                              byrow = TRUE))

r ggplot2 alignment legend
1个回答
0
投票

我不完全理解你的问题。您想让顺序为 3、5、4 吗?或者您希望它们全部排成一排?这是您正在寻找的输出吗?你能澄清一下吗?

ggplot(mtcars %>% 
         mutate(gear = factor(gear, levels = c(3, 5, 4)))) +
  aes(x = disp,
      y = qsec,
      shape = gear,
      color = gear) +
  geom_line() +
  geom_point() +
  theme(legend.position = "bottom",
        legend.title = element_blank())

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