在图例项之间添加水平空间

Mas*_*013 4 r ggplot2

我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))
Run Code Online (Sandbox Code Playgroud)

但结果项目太近了: 在此输入图像描述

有什么建议吗?

zx8*_*754 5

有点黑客:

#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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述