How can I increase the space between the keys of the legend of ggplot2
plot?
如何增加ggplot2图图例之间的空格?
library(ggplot2)
ggplot(aes(mpg, wt, colour = factor(cyl)),
, data = mtcars) +
geom_point() +
theme(legend.direction = "horizontal",
legend.position = "bottom") +
guides(color = guide_legend(nrow=2))
I am looking for a ggplot2 option that add a kind of vertical adjustment between (key 4 and key 6) in the plot above? Should I create a custom legend key?
我正在寻找一个ggplot2选项,添加一种垂直调整之间(关键4和关键6)在图上?我应该创建一个自定义的图例键吗?
PS: I want to increase the blank space between boxes not between labels.
PS:我想增加盒子之间的空白,而不是标签之间的空白。
the desired plot is :
期望的情节是:
NOTE: No the question is not duplicated of the other question. We want here to add a vertical spacing between items that are already in multiple rows. In the other question we have 1-row legend and we want to add spaces (horizontal) between items.
注意:不,这个问题和另一个问题不重复。在这里,我们希望在已经在多行中的项之间添加垂直间距。在另一个问题中,我们有一行图例,我们想在项目之间添加空格(水平)。
2 个解决方案
#1
46
An alternative (and probably easier) solution is using legend.key
and legend.key.size
in the theme
part of your code:
另一种解决方案是使用legend。键和legend.key。代码主题部分的大小:
ggplot(data = mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() +
guides(color = guide_legend(nrow = 2)) +
theme(legend.direction = 'horizontal',
legend.position = 'bottom',
legend.key = element_rect(size = 5),
legend.key.size = unit(1.5, 'lines'))
this gives:
这给:
In case you are calling theme_bw
or theme_classic
before manipulating the legend, you should set the color of the legend rectangle:
如果您在操作传奇之前调用theme_bw或theme_classic,您应该设置legend矩形的颜色:
legend.key = element_rect(size = 5, color = 'white') #or: color = NA
#2
7
Here a solution using gtable
. Basically I am extracting legend grobs table and I add a row in the legend table.
这里有一个使用gtable的解决方案。基本上,我提取了legend grobs表,并在legend表中添加了一行。
library(gtable)
library(grid)
## transform the ggplot to a grobs table
p_table <- ggplot_gtable(ggplot_build(p))
## extract legend
leg <- which(sapply(p_table$grobs, function(x) x$name) == "guide-box")
## this is the tricky part !
## add a row in the second position (pos=2)
p_table$grobs[[leg]]$grobs[[1]] <-
gtable_add_rows(p_table$grobs[[leg]]$grobs[[1]],
unit(0.5, "line"), ## you can increase the height here
pos=2) ## since I have 2 rows , I insert it in the middle
plot(p_table)
PS: I dont' know here how to coerce the table to a plot again! maybe someone else can help here ( I am just plotting it and losing the object structure)
PS:我不知道怎么再把桌子硬塞进阴谋!也许有人能帮上忙(我只是在画它然后失去对象结构)
#1
46
An alternative (and probably easier) solution is using legend.key
and legend.key.size
in the theme
part of your code:
另一种解决方案是使用legend。键和legend.key。代码主题部分的大小:
ggplot(data = mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() +
guides(color = guide_legend(nrow = 2)) +
theme(legend.direction = 'horizontal',
legend.position = 'bottom',
legend.key = element_rect(size = 5),
legend.key.size = unit(1.5, 'lines'))
this gives:
这给:
In case you are calling theme_bw
or theme_classic
before manipulating the legend, you should set the color of the legend rectangle:
如果您在操作传奇之前调用theme_bw或theme_classic,您应该设置legend矩形的颜色:
legend.key = element_rect(size = 5, color = 'white') #or: color = NA
#2
7
Here a solution using gtable
. Basically I am extracting legend grobs table and I add a row in the legend table.
这里有一个使用gtable的解决方案。基本上,我提取了legend grobs表,并在legend表中添加了一行。
library(gtable)
library(grid)
## transform the ggplot to a grobs table
p_table <- ggplot_gtable(ggplot_build(p))
## extract legend
leg <- which(sapply(p_table$grobs, function(x) x$name) == "guide-box")
## this is the tricky part !
## add a row in the second position (pos=2)
p_table$grobs[[leg]]$grobs[[1]] <-
gtable_add_rows(p_table$grobs[[leg]]$grobs[[1]],
unit(0.5, "line"), ## you can increase the height here
pos=2) ## since I have 2 rows , I insert it in the middle
plot(p_table)
PS: I dont' know here how to coerce the table to a plot again! maybe someone else can help here ( I am just plotting it and losing the object structure)
PS:我不知道怎么再把桌子硬塞进阴谋!也许有人能帮上忙(我只是在画它然后失去对象结构)