简体   繁体   English

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

[英]Spacing between the legend keys for multiple legends in ggplot2 in R

I searched through internet and I couldn't find a solution for my issue. 我通过互联网搜索,我无法找到解决问题的方法。

So to make it more solid say that we have a graph with two legends like below: 所以为了让它更加坚实,我们有一个图表,其中有两个传说,如下所示:

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)

在此输入图像描述

I can change the spacing for the first legend group related with size by using size option in guides . 我可以使用guides size选项更改与尺寸相关的第一个图例组的间距。 However, for the second group which indicates color, I can't neither make the whole group closer to the graph or reduce to size between the colored circles. 然而,对于指示颜色的第二组,我既不能使整个组更接近图形,也不能减小颜色圆之间的尺寸。

I have also tried the legend options in theme such as legend.spacing.x/y and legend.key.width/height . 我还尝试了主题中的图例选项,例如legend.spacing.x/ylegend.key.width/height These options only worked for the first legend group. 这些选项仅适用于第一个图例组。

Is there a way to reduce the sizes between the different color keys? 有没有办法减少不同颜色键之间的大小? Changing the sizes of the keys would be also good to discover. 改变键的大小也很好发现。

To make my request clearer, here is the gaps that I want to adjust: 为了使我的请求更清楚,我想调整的差距是: 在此输入图像描述

Thanks in advance. 提前致谢。

I am not exactly sure what you need, but I think you want the points in the legend be smaller. 我不确定你需要什么,但我认为你希望传奇中的点数更小。 In that case, override.aes() is the function you need. 在这种情况下, override.aes()是您需要的功能。

If your question is different, please clarify further so we can help you. 如果您的问题不同,请进一步澄清,以便我们为您提供帮助。

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()

Created on 2019-07-08 by the reprex package (v0.3.0) reprex包创建于2019-07-08(v0.3.0)

Legend spacing is always kind of an issue and gets many votes eg here . 传奇间距总是一个问题,并获得很多投票, 例如在这里 I think one of the issues in your particular example could be the continuous nature of gear . 我认为你的特定例子中的一个问题可能是gear的连续性。 Factorising might help (if you have more values than gear , use cut() instead) and then change the legend spacing within your guides call using unit() . 因子分解可能会有所帮助(如果您有更多的值而不是gear ,请使用cut()代替)然后使用unit()更改guides调用中的图例间距。 I have boiled down your code a bit and also replace title = '' with title = NULL , as otherwise ggplot is actually drawing an empty object. 我已经简化了你的代码,并用title = NULL替换title = '' ,否则ggplot实际上是在绘制一个空对象。

library(tidyverse)

mtcars_f <- mtcars %>% mutate(gear_f = factor(gear)) #factorising gear

ggplot(mtcars_f, aes(disp, mpg)) + 
  geom_point(aes(color = gear_f), size = 10) + 
  geom_line(aes(size = disp/mpg*100)) +
  guides(size = guide_legend(title = NULL, order = 1, 
         keyheight = unit(0.1, 'inch')), 
         color = guide_legend(title = NULL, order = 2, 
         keyheight = unit(0.1, 'inch'))) +
  scale_color_brewer(palette = 'Blues') +
  theme(legend.key= element_blank())

Created on 2019-07-19 by the reprex package (v0.3.0) reprex包创建于2019-07-19(v0.3.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM