有没有办法在ggplot2中更改图例项之间的间距?我现在有
legend.position ="top"
它会自动生成水平图例 . 然而,物品的间距非常接近,我想知道如何将它们分开更远 .
使用其中任何一个
legend.spacing = unit(1,"cm") legend.spacing.x = unit(1,"cm") legend.spacing.y = unit(1,"cm")
我认为最好的选择是在 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
我用来在水平图例中添加空格的简单修复,只需在标签中添加空格(参见下面的摘录):
scale_fill_manual(values=c("red","blue","white"), labels=c("Label of category 1 ", "Label of category 2 ", "Label of category 3"))
既然 ggplot2 在 ggplot2 包中已弃用,则应使用函数 theme :
ggplot2
theme
library(grid) # for unit() ... + theme(legend.key.height=unit(3,"line")) ... + theme(legend.key.width=unit(3,"line"))
2018年7月发布的ggplot2 v3.0.0具有修改 legend.spacing.x , legend.spacing.y 和 legend.text 的工作选项 .
legend.spacing.x
legend.spacing.y
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_viridis_d("Cyl") + theme(legend.position = 'top', legend.spacing.x = unit(1.0, 'cm'))
注意:如果您只想展开图例文本右侧的间距,请使用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_viridis_d("Cyl") + 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(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 ,设置 legend.key.size 只会增加图例键的大小,而不会增加它们之间的垂直空间
legend.key.size
ggplot(mtcars) + aes(fill = factor(cyl), x = cyl) + geom_bar() + theme(legend.key.size = unit(1, "cm"))
为了增加图例键之间的距离,需要修改 legend-draw.r 函数 . 有关详细信息,请参阅issue
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" )) } # register new key drawing function, # the effect is global & persistent throughout the R session GeomBar$draw_key = draw_key_polygon3 ggplot(mtcars) + aes(fill = factor(cyl), x = cyl) + geom_bar() + theme(legend.key = element_rect(color = NA, fill = NA), legend.key.size = unit(1.5, "cm"))
要在图例中的条目之间添加间距,请调整主题元素 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
[关于编辑的注意事项:当这个答案首次发布时,有一个错误 . 该错误现已修复]
来自Koshke关于ggplot2的工作和他的博客(Koshke's blog)
... + 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()
看起来最好的方法(在2018年)是在 theme 对象下使用 legend.key.size . (例如,见here) .
#Set-up: library(ggplot2) library(gridExtra) gp <- ggplot(data = mtcars, aes(mpg, cyl, colour = factor(cyl))) + geom_point()
这是 real easy if you are using 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)
但是,这个 doesn't work quite so well otherwise (例如,如果您需要图例符号的灰色背景):
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
请注意,白色方块开始阻止图例 Headers (如果我们继续增加值,最终会自动生成图形) .
#This shows you why: gt <- gp2 + theme(legend.key = element_rect(size = 10,color = 'yellow' ))
我还没有找到解决上述问题的解决办法......如果您有任何想法,请在评论中告诉我,我会相应更新!
$layers
8 回答
使用其中任何一个
我认为最好的选择是在
guides
中使用guide_legend
:注意使用
default.unit
,无需加载grid
包 .我用来在水平图例中添加空格的简单修复,只需在标签中添加空格(参见下面的摘录):
既然
ggplot2
在ggplot2
包中已弃用,则应使用函数theme
:2018年7月发布的ggplot2 v3.0.0具有修改
legend.spacing.x
,legend.spacing.y
和legend.text
的工作选项 .Example: Increase horizontal spacing between legend keys
注意:如果您只想展开图例文本右侧的间距,请使用stringr::str_pad()
Example: Move the legend key labels to the bottom and increase vertical spacing
Example: for scale_fill_xxx & guide_colorbar
For vertical legends ,设置
legend.key.size
只会增加图例键的大小,而不会增加它们之间的垂直空间为了增加图例键之间的距离,需要修改
legend-draw.r
函数 . 有关详细信息,请参阅issue要在图例中的条目之间添加间距,请调整主题元素
legend.text
的边距 .要在每个图例标签的右侧添加30pt的空间(可能对水平图例有用):
要在每个图例标签的左侧添加30pt的空间(可能对垂直图例有用):
对于
ggplot2
对象p
. 关键字是legend.text
和margin
.[关于编辑的注意事项:当这个答案首次发布时,有一个错误 . 该错误现已修复]
来自Koshke关于ggplot2的工作和他的博客(Koshke's blog)
在控制台中键入
theme_get()
以查看其他可编辑的图例属性 .看起来最好的方法(在2018年)是在
theme
对象下使用legend.key.size
. (例如,见here) .这是 real easy if you are using theme_bw():
但是,这个 doesn't work quite so well otherwise (例如,如果您需要图例符号的灰色背景):
请注意,白色方块开始阻止图例 Headers (如果我们继续增加值,最终会自动生成图形) .
我还没有找到解决上述问题的解决办法......如果您有任何想法,请在评论中告诉我,我会相应更新!
$layers
重新分层...