有没有办法改变ggplot2中图例项之间的间距? [英] Is there a way to change the spacing between legend items in ggplot2?

查看:31
本文介绍了有没有办法改变ggplot2中图例项之间的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变ggplot2中图例项之间的间距?我目前有

legend.position ="top"

自动生成水平图例.但是,项目的间距非常接近,我想知道如何将它们分开.

解决方案

示例:将图例键标签移到底部并增加垂直间距

ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +geom_bar() +coord_flip() +scale_fill_brewer("Cyl", 调色板 = "Dark2") +主题最小(base_size = 14)+主题(legend.position = 'top',Legend.spacing.x = unit(1.0, 'cm'),Legend.text = element_text(margin = margin(t = 10))) +指南(填充 = guide_legend(标题 = Cyl",label.position =底部",title.position = 左",title.vjust = 1))

示例:对于 scale_fill_xxx &guide_colorbar

ggplot(mtcars, aes(mpg, wt)) +geom_point(aes(fill = hp), pch = I(21), size = 5)+scale_fill_viridis_c(guide = FALSE) +主题经典(base_size = 14)+主题(legend.position = 'top',Legend.spacing.x = unit(0.5, 'cm'),Legend.text = element_text(margin = margin(t = 10))) +指南(填充 = guide_colorbar(标题 = HP",label.position =底部",title.position = "left", title.vjust = 1,# 在图例周围绘制边框frame.colour = 黑色",条宽 = 15,条形高度 = 1.5))


对于垂直图例,设置legend.key.size只会增加图例键的大小,而不是它们之间的垂直间距

ggplot(mtcars) +aes(x = cyl, 填充 = 因子(cyl)) +geom_bar() +scale_fill_brewer("Cyl", 调色板 = "Dark2") +主题最小(base_size = 14)+主题(legend.key.size = unit(1,cm"))

为了增加图例键之间的距离,需要修改legend-draw.r函数.请参阅此问题了解更多信息

# 增加图例键之间垂直间距的函数#@克劳斯维尔克draw_key_polygon3 <- 函数(数据,参数,大小){lwd <- min(data$size, min(size)/4)网格::rectGrob(width = grid::unit(0.6, npc"),height = grid::unit(0.6, npc"),gp = 网格::gpar(col = 数据$颜色,填充 = alpha(data$fill, data$alpha),lty = 数据$线型,lwd = lwd * .pt,linejoin = "mitre";))}### 根据下面的 tjebo 评论,不再需要此步骤###另见:https://ggplot2.tidyverse.org/reference/draw_key.html# 注册新的按键绘制函数,# 效果是全局的 &在整个 R 会话中保持不变# GeomBar$draw_key = draw_key_polygon3ggplot(mtcars) +aes(x = cyl, 填充 = 因子(cyl)) +geom_bar(key_glyph = "polygon3") +scale_fill_brewer("Cyl", 调色板 = "Dark2") +主题最小(base_size = 14)+主题(legend.key = element_rect(颜色= NA,填充= NA),Legend.key.size = unit(1.5, cm")) +主题(legend.title.align = 0.5)

Is there a way to change the spacing between legend items in ggplot2? I currently have

legend.position ="top" 

which automatically produces a horizontal legend. However, the spacing of the items is very close together and I am wondering how to space them farther apart.

解决方案

ggplot2 v3.0.0 released in July 2018 has working options to modify legend.spacing.x, legend.spacing.y and legend.text.

Example: Increase horizontal spacing between legend keys

library(ggplot2)

ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) + 
  geom_bar() +
  coord_flip() +
  scale_fill_brewer("Cyl", palette = "Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'top', 
        legend.spacing.x = unit(1.0, 'cm'))

Note: If you only want to expand the spacing to the right of the legend text, use stringr::str_pad()

Example: Move the legend key labels to the bottom and increase vertical spacing

ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) + 
  geom_bar() +
  coord_flip() +
  scale_fill_brewer("Cyl", palette = "Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'top', 
        legend.spacing.x = unit(1.0, 'cm'),
        legend.text = element_text(margin = margin(t = 10))) +
  guides(fill = guide_legend(title = "Cyl",
                             label.position = "bottom",
                             title.position = "left", title.vjust = 1)) 

Example: for scale_fill_xxx & guide_colorbar

ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(fill = hp), pch = I(21), size = 5)+
  scale_fill_viridis_c(guide = FALSE) +
  theme_classic(base_size = 14) +
  theme(legend.position = 'top', 
        legend.spacing.x = unit(0.5, 'cm'),
        legend.text = element_text(margin = margin(t = 10))) +
  guides(fill = guide_colorbar(title = "HP",
                               label.position = "bottom",
                               title.position = "left", title.vjust = 1,
                               # draw border around the legend
                               frame.colour = "black",
                               barwidth = 15,
                               barheight = 1.5)) 


For vertical legends, settinglegend.key.size only increases the size of the legend keys, not the vertical space between them

ggplot(mtcars) +
  aes(x = cyl, fill = factor(cyl)) +
  geom_bar() +
  scale_fill_brewer("Cyl", palette = "Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.key.size = unit(1, "cm"))

In order to increase the distance between legend keys, modification of the legend-draw.r function is needed. See this issue for more info

# function to increase vertical spacing between legend keys
# @clauswilke
draw_key_polygon3 <- function(data, params, size) {
  lwd <- min(data$size, min(size) / 4)
  
  grid::rectGrob(
    width = grid::unit(0.6, "npc"),
    height = grid::unit(0.6, "npc"),
    gp = grid::gpar(
      col = data$colour,
      fill = alpha(data$fill, data$alpha),
      lty = data$linetype,
      lwd = lwd * .pt,
      linejoin = "mitre"
    ))
}

### this step is not needed anymore per tjebo's comment below
### see also: https://ggplot2.tidyverse.org/reference/draw_key.html
# register new key drawing function, 
# the effect is global & persistent throughout the R session
# GeomBar$draw_key = draw_key_polygon3

ggplot(mtcars) +
  aes(x = cyl, fill = factor(cyl)) +
  geom_bar(key_glyph = "polygon3") +
  scale_fill_brewer("Cyl", palette = "Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.key = element_rect(color = NA, fill = NA),
        legend.key.size = unit(1.5, "cm")) +
  theme(legend.title.align = 0.5)

这篇关于有没有办法改变ggplot2中图例项之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆