R中ggplot2中多个图例的图例键之间的间距

DSA*_*DSA 5 r legend ggplot2 legend-properties

我通过互联网搜索,但找不到解决我问题的方法。

为了更明确地说,我们有一个带有两个图例的图形,如下所示:

library(ggplot2)

ggplot() + 
  geom_point(data = mtcars, aes(x = disp, y = mpg, color = gear), 
             pch =20, size=18) + 
  geom_line(data = mtcars, aes(x = disp, y = mpg, size = disp/mpg*100)) +
  scale_size(range = c(0,3.5)) +
  guides(size = guide_legend("", order = 1, keywidth = 2, keyheight = 1.5), 
         color = guide_legend("", order = 2, keywidth = 1, keyheight = 1 )) +
  labs(x = "disp", y = "mpg") +
  geom_text(size=2.7, color = "grey29",  vjust=-0.8) +
  theme_bw() 

# ggsave("trial.png", width = 11.5, height = 8.5)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我可以使用中的size选项更改与尺寸相关的第一个图例组的间距 guides。但是,对于表示颜色的第二组,既不能使整个组更接近图形,也不能缩小彩色圆圈之间的大小。

我还尝试了诸如legend.spacing.x/y和主题中的图例选项legend.key.width/height。这些选项仅适用于第一个图例组。

有没有办法减小不同颜色键之间的大小?更改键的大小也很容易发现。

为了使我的要求更加明确,这是我要调整的差距: 在此处输入图片说明

提前致谢。

M--*_*M-- 1

我不太确定您需要什么,但我认为您希望图例中的点更小。在这种情况下,override.aes()这就是您需要的功能。

如果您的问题不同,请进一步澄清,以便我们为您提供帮助。

library(ggplot2)

ggplot() + 
  geom_point(data = mtcars, aes(x = disp, y = mpg, color = gear), 
             pch =20, size=18) + 
  geom_line(data = mtcars, aes(x = disp, y = mpg, size = disp/mpg*100)) +
  scale_size(range = c(0,3.5)) +
  guides(size = guide_legend("", order = 1, keywidth = 2, keyheight = 1.5), 
         color = guide_legend("", order = 2, keywidth = 1, keyheight = 1, 
                              override.aes = list(size=9))) +
  labs(x = "disp", y = "mpg") +
  geom_text(size=2.7, color = "grey29",  vjust=-0.8) +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2019-07-08 创建