Is there a way to change the spacing between legend items in ggplot2?
是否可以更改ggplot2中图例项之间的间距?我目前有
1 | legend.position ="top" |
会自动生成水平图例。但是,这些项目的间距非常接近,我想知道如何将它们分开得更远。
2018年7月发布的
示例:增加图例键之间的水平间距
1 2 3 4 5 6 7 8 9 | 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')) |
1 2 3 4 5 6 7 8 9 10 11 | 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)) |
我认为最好的选择是在
1 2 3 4 5 | p + guides(fill=guide_legend( keywidth=0.1, keyheight=0.1, default.unit="inch") ) |
请注意使用
一个简单的修复方法,我用于在水平图例中添加空间,只需在标签中添加空间(请参见下面的摘录):
1 2 3 4 | scale_fill_manual(values=c("red","blue","white"), labels=c("Label of category 1 ", "Label of category 2 ", "Label of category 3")) |
现在
1 2 3 | library(grid) # for unit() ... + theme(legend.key.height=unit(3,"line")) ... + theme(legend.key.width=unit(3,"line")) |
要在图例中的条目之间增加间距,请调整主题元素
在每个图例标签的右侧添加30pt的空间(对于水平图例可能有用):
1 2 | p + theme(legend.text = element_text( margin = margin(r = 30, unit ="pt"))) |
在每个图例标签的左侧添加30pt的空间(对于垂直图例可能有用):
1 2 | p + theme(legend.text = element_text( margin = margin(l = 30, unit ="pt"))) |
用于
[有关编辑的注意事项:首次发布此答案时,存在一个错误。该错误现已修复]
看起来最好的方法(在2018年)是在
1 2 3 4 5 6 | #Set-up: library(ggplot2) library(gridExtra) gp <- ggplot(data = mtcars, aes(mpg, cyl, colour = factor(cyl))) + geom_point() |
如果使用
1 2 3 4 5 6 7 8 9 | 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) |
但是,这在其他情况下效果不佳(例如,如果您的图例符号上需要灰色背景):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
请注意,白色方块开始阻塞图例标题(如果我们继续增加该值,则最终变为图形本身)。
1 2 | #This shows you why: gt <- gp2 + theme(legend.key = element_rect(size = 10,color = 'yellow' )) |
我还没有找到解决上述问题的解决方法...
如果您有任何想法,请在评论中让我知道,我会进行相应的更新!
-
我想知道是否有某种方法可以使用
$layers 重新分层...
来自Koshke在ggplot2上的工作及其博客(Koshke的博客)
1 2 | ... + theme(legend.key.height=unit(3,"line")) # Change 3 to X ... + theme(legend.key.width=unit(3,"line")) # Change 3 to X |
在控制台中键入
使用其中任何一个
1 2 3 | legend.spacing = unit(1,"cm") legend.spacing.x = unit(1,"cm") legend.spacing.y = unit(1,"cm") |