是否有办法改变 ggplot2中图例项之间的间距
legend.position ="top"
它会自动生成一个水平图例。然而,项目的间距是非常接近在一起,我想知道如何空间他们更远的距离。
来自 Koshke 的 ggplot2和他的博客(Koshke 的博客)
... + theme(legend.key.height=unit(3,"line")) # Change 3 to X ... + theme(legend.key.width=unit(3,"line")) # Change 3 to X
在控制台中键入 theme_get()以查看其他可编辑的图例属性。
theme_get()
既然 opts在 ggplot2软件包中已被弃用,那么应该使用函数 theme:
opts
ggplot2
theme
library(grid) # for unit() ... + theme(legend.key.height=unit(3,"line")) ... + theme(legend.key.width=unit(3,"line"))
一个简单的修正,我用来添加水平图例空间,只需在标签中添加空格(见下面的摘录) :
scale_fill_manual(values=c("red","blue","white"), labels=c("Label of category 1 ", "Label of category 2 ", "Label of category 3"))
我认为最好的选择是在 guides中使用 guide_legend:
guides
guide_legend
p + guides(fill=guide_legend( keywidth=0.1, keyheight=0.1, default.unit="inch") )
注意使用 default.unit,无需加载 grid包。
default.unit
grid
若要在图例中添加条目之间的间距,请调整主题元素 legend.text的边距。
legend.text
在每个图例标签的右侧添加30pt 的空间(可能对水平图例有用) :
p + theme(legend.text = element_text( margin = margin(r = 30, unit = "pt")))
在每个图例标签的左侧添加30pt 的空间(可能对垂直图例有用) :
p + theme(legend.text = element_text( margin = margin(l = 30, unit = "pt")))
对于 ggplot2对象 p。关键字是 legend.text和 margin。
p
margin
[关于编辑的注意事项: 当这个答案第一次发布的时候,有一个错误。这个错误现在已经被修复了]
这些随便用
legend.spacing = unit(1,"cm") legend.spacing.x = unit(1,"cm") legend.spacing.y = unit(1,"cm")
看起来最好的方法(在2018年)是在 theme对象下使用 legend.key.size。
legend.key.size
#Set-up: library(ggplot2) library(gridExtra) gp <- ggplot(data = mtcars, aes(mpg, cyl, colour = factor(cyl))) + geom_point()
这是 如果你使用的是 theme_bw():
theme_bw()
gpbw <- gp + theme_bw() #Change spacing size: g1bw <- gpbw + theme(legend.key.size = unit(0, 'lines')) g2bw <- gpbw + theme(legend.key.size = unit(1.5, 'lines')) g3bw <- gpbw + theme(legend.key.size = unit(3, 'lines')) grid.arrange(g1bw,g2bw,g3bw,nrow=3)
但是,这个 否则就没那么好用了(例如,如果你需要图例符号的灰色背景) :
g1 <- gp + theme(legend.key.size = unit(0, 'lines')) g2 <- gp + theme(legend.key.size = unit(1.5, 'lines')) g3 <- gp + theme(legend.key.size = unit(3, 'lines')) grid.arrange(g1,g2,g3,nrow=3) #Notice that the legend symbol squares get bigger (that's what legend.key.size does). #Let's [indirectly] "control" that, too: gp2 <- g3 g4 <- gp2 + theme(legend.key = element_rect(size = 1)) g5 <- gp2 + theme(legend.key = element_rect(size = 3)) g6 <- gp2 + theme(legend.key = element_rect(size = 10)) grid.arrange(g4,g5,g6,nrow=3) #see picture below, left
请注意,白色正方形开始阻塞图例标题(并最终图本身,如果我们继续增加值)。
#This shows you why: gt <- gp2 + theme(legend.key = element_rect(size = 10,color = 'yellow' ))
我还没有找到解决上述问题的办法..。 如果你有什么想法,请在评论中告诉我,我会相应地更新!
$layers
2018年7月发布的 ggplot2 v3.0.0 有修改 legend.spacing.x、 legend.spacing.y和 legend.text的工作选项。
ggplot2 v3.0.0
legend.spacing.x
legend.spacing.y
更新2021年12月-使 legend.spacing.y工作,您将需要设置相应的指南 _ 图例中的 byrow = TRUE。看看这个帖子.下面的例子。
byrow = TRUE
示例: 增加图例键之间的水平间距
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'))
注意: 如果只想扩展图例文本右侧的间距,请使用 stringr::str_pad()
stringr::str_pad()
示例: 增加垂直间距(注意 byrow = TRUE)
library(ggplot2) ggplot(mtcars, aes(y = factor(cyl), fill = factor(cyl))) + geom_bar() + theme(legend.spacing.y = unit(1.0, 'cm')) + ## important additional element guides(fill = guide_legend(byrow = TRUE))
示例: 将图例键标签移到底部并增加垂直间距
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))
例如: 对于 scale_fill_xxx和 guide_colorbar
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))
下面的内容已经过时了,但是留给好奇的人们。
对于垂直图例 ,设置 legend.key.size只会增加图例键的大小,而不会增加它们之间的垂直空间
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"))
为了增加图例键之间的距离,需要修改 legend-draw.r函数。更多信息请参见 问题
legend-draw.r
# 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)