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

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

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


legend.position ="top" 

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


慕仙森
浏览 6897回答 4
4回答

有只小跳蛙

ggplot2 v3.0.0于2018年7月发布的版本具有修改的工作选项legend.spacing.x,legend.spacing.y并且legend.text。示例:增加图例键之间的水平间距library(ggplot2)ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +&nbsp;&nbsp; geom_bar() +&nbsp; coord_flip() +&nbsp; scale_fill_brewer("Cyl", palette = "Dark2") +&nbsp; theme_minimal(base_size = 14) +&nbsp; theme(legend.position = 'top',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; legend.spacing.x = unit(1.0, 'cm'))注意:如果只想在图例文本的右侧扩展间距,请使用 stringr::str_pad()示例:将图例键标签移至底部并增加垂直间距ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +&nbsp;&nbsp; geom_bar() +&nbsp; coord_flip() +&nbsp; scale_fill_brewer("Cyl", palette = "Dark2") +&nbsp; theme_minimal(base_size = 14) +&nbsp; theme(legend.position = 'top',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; legend.spacing.x = unit(1.0, 'cm'),&nbsp; &nbsp; &nbsp; &nbsp; legend.text = element_text(margin = margin(t = 10))) +&nbsp; guides(fill = guide_legend(title = "Cyl",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label.position = "bottom",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title.position = "left", title.vjust = 1))&nbsp;示例:用于scale_fill_xxx&guide_colorbarggplot(mtcars, aes(mpg, wt)) +&nbsp; geom_point(aes(fill = hp), pch = I(21), size = 5)+&nbsp; scale_fill_viridis_c(guide = FALSE) +&nbsp; theme_classic(base_size = 14) +&nbsp; theme(legend.position = 'top',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; legend.spacing.x = unit(0.5, 'cm'),&nbsp; &nbsp; &nbsp; &nbsp; legend.text = element_text(margin = margin(t = 10))) +&nbsp; guides(fill = guide_colorbar(title = "HP",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label.position = "bottom",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title.position = "left", title.vjust = 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# draw border around the legend&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;frame.colour = "black",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;barwidth = 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;barheight = 1.5))&nbsp;对于垂直图例,设置legend.key.size只会增加图例键的大小,而不会增加它们之间的垂直间隔ggplot(mtcars) +&nbsp; aes(x = cyl, fill = factor(cyl)) +&nbsp; geom_bar() +&nbsp; scale_fill_brewer("Cyl", palette = "Dark2") +&nbsp; theme_minimal(base_size = 14) +&nbsp; theme(legend.key.size = unit(1, "cm"))为了增加图例键之间的距离,需要修改legend-draw.r功能。有关更多信息,请参见此问题# function to increase vertical spacing between legend keys# @clauswilkedraw_key_polygon3 <- function(data, params, size) {&nbsp; lwd <- min(data$size, min(size) / 4)&nbsp; grid::rectGrob(&nbsp; &nbsp; width = grid::unit(0.6, "npc"),&nbsp; &nbsp; height = grid::unit(0.6, "npc"),&nbsp; &nbsp; gp = grid::gpar(&nbsp; &nbsp; &nbsp; col = data$colour,&nbsp; &nbsp; &nbsp; fill = alpha(data$fill, data$alpha),&nbsp; &nbsp; &nbsp; lty = data$linetype,&nbsp; &nbsp; &nbsp; lwd = lwd * .pt,&nbsp; &nbsp; &nbsp; linejoin = "mitre"&nbsp; &nbsp; ))}# register new key drawing function,&nbsp;# the effect is global & persistent throughout the R sessionGeomBar$draw_key = draw_key_polygon3ggplot(mtcars) +&nbsp; aes(x = cyl, fill = factor(cyl)) +&nbsp; geom_bar() +&nbsp; scale_fill_brewer("Cyl", palette = "Dark2") +&nbsp; theme_minimal(base_size = 14) +&nbsp; theme(legend.key = element_rect(color = NA, fill = NA),&nbsp; &nbsp; &nbsp; &nbsp; legend.key.size = unit(1.5, "cm")) +&nbsp; theme(legend.title.align = 0.5)

天涯尽头无女友

我认为最好的选择是guide_legend在内使用guides:p + guides(fill=guide_legend(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;keywidth=0.1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;keyheight=0.1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default.unit="inch")&nbsp; &nbsp; &nbsp; )注意使用default.unit,无需加载grid程序包。

精慕HU

我用于在水平图例中添加空间的简单解决方案,只需在标签中添加空间(请参见下面的摘录):&nbsp; scale_fill_manual(values=c("red","blue","white"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels=c("Label of category 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Label of category 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Label of category 3"))

慕斯709654

要在图例中的条目之间增加间距,请调整主题元素的边距legend.text。要在每个图例标签的右侧添加30pt的空间(对于水平图例可能有用):p + theme(legend.text = element_text(&nbsp; &nbsp; margin = margin(r = 30, unit = "pt")))在每个图例标签的左侧添加30pt的空间(对于垂直图例可能有用):p + theme(legend.text = element_text(&nbsp; &nbsp; margin = margin(l = 30, unit = "pt")))对于一个ggplot2对象p。关键字是legend.text和margin。[有关编辑的注意事项:首次发布此答案时,存在一个错误。该错误现已修复]
打开App,查看更多内容
随时随地看视频慕课网APP