我在 ggplot2 的图表底部有一个图例,分两行。我需要增加项目之间的水平空间。

目前代码是:

p + theme(legend.key = element_blank(),
       legend.position = "bottom",
       legend.title = element_blank(),
       legend.direction = "horizontal") +
    guides(linetype = guide_legend(ncol = 3,keywidth=4))

但产生的项目太接近了:

有什么建议吗?

最佳答案

有点黑客:

#dummy data
df <- data.frame(x=1:20,
                 y=runif(20),
                 g=rep(c("a","long1","looonger1","xx"),5))


#suffix with spaces, make them same length
df$g <- substring(paste0(df$g,"                 "),1,15)

#plot as usual
ggplot(df,aes(x,y,linetype=g)) +
  geom_line() +
  theme(legend.key = element_blank(),
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.direction = "horizontal") +
  guides(linetype = guide_legend(ncol = 3,keywidth=4))

关于r - 在图例项之间添加水平间距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29953629/

10-12 18:13