[英]Add more space between ggplot legend items
I have data like this 我有这样的数据
plot.d <- data.frame(vars = c(11:20),
time = c(1:5,1:5),
method = c(rep("aaaa", 5), rep("bbbb", 5)))
Now I want to create a point plot, the legend should be 1 column, but with increased space. 现在,我想创建一个点图,图例应为1列,但要增加空间。
According to the manual, legend.spacing.y
should do the trick. 根据手册,
legend.spacing.y
应该可以解决问题。 However, using this code: 但是,使用此代码:
ggplot(plot.d, aes(y = vars, x = time)) +
geom_point(aes(shape= method, color = method, size = method)) +
scale_shape_manual(values=c(18,15), guide=guide_legend(nrow=2)) +
scale_color_manual(values=c('grey60','grey50')) +
scale_size_manual(values=c(3, 2)) +
theme(legend.title = element_blank(),
legend.spacing.x = unit(0.15, 'cm'),
legend.spacing.y = unit(1.4, 'cm'),
legend.text=element_text(size=12),
legend.box.background = element_rect(colour = "black")
)
I only get this plot: 我只得到这个情节:
The box around the legend is increased but the line spacing between the two legend items remains the same. 图例周围的框增加了,但两个图例项之间的行间距保持不变。
What's wrong here? 怎么了
If I got the aim well, you need to modify legend.key.*
. 如果我的目标很好,则需要修改
legend.key.*
。 An example with random choices: 随机选择的示例:
ggplot(plot.d, aes(y = vars, x = time)) +
geom_point(aes(shape= method, color = method, size = method)) +
scale_shape_manual(values=c(18,15), guide=guide_legend(nrow=2)) +
scale_color_manual(values=c('grey60','grey50')) +
scale_size_manual(values=c(3, 2)) +
theme(legend.title = element_blank(),
legend.spacing.x = unit(0.15, 'cm'),
legend.spacing.y = unit(1.2, 'cm'),
legend.text=element_text(size=12),
legend.key.width = unit(1.5,"cm"),
legend.key.height = unit(1.2,"cm"),
legend.box.background = element_rect(colour = "black")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.