简体   繁体   English

增加 ggplot2 中图例键之间的垂直间距

[英]Increase Vertical Spacing between Legend Key in ggplot2

How can I increase vertical spacing between legend keys:如何增加图例键之间的垂直间距:

p1 <- ggplot(data = HSS, mapping = aes(x = EVENT, y = HSS, fill = TIME)) + 
            geom_bar(stat = "identity",width=0.7, colour = "black", position = position_dodge(0.7)) + 
            scale_fill_manual("HSS", values = c("deepskyblue3", "indianred2"), 
            labels = c("1200 UTC  (0.049)", "0000 UTC  (0.031)")) + theme_bw()

p1 <- p1 + scale_y_continuous(expand = expansion(mult = c(0.0085, -0.085)), 
           limits = c(-0.03,0.5), breaks = c(-0.03,-0.01, 0.01, 0.03, 0.05, 0.07,0.09,0.11,0.13,0.15,0.17,
                                             0.19, 0.21,0.23,0.25,0.27,0.29,0.31,0.33,0.45),
           
           labels = c("-0.03","-0.01","0.01","0.03","0.05","0.07","0.09","0.11","0.13","0.15","0.17",
                      "0.19","0.21","0.23","0.25","0.27","0.29","0.31","0.33","0.45")) + 
  
          theme(axis.text.x=element_text(color = "black", size=12, face = "bold", angle=90, vjust=.5,
                 hjust=0.8)) + 
          theme(axis.text.y = element_text(color = "black", size=12, face = "bold"))




p1 <- p1 + theme( axis.line = element_line(colour = "black", size = 0.5, linetype = "solid")) + 
                          labs( y = "HSS")


p1 <- p1 + theme(axis.title=element_text(colour = "blue2"  ,size=14,face="bold", vjust = 0.1))

p1 <-  p1 +  theme(legend.position=c(0.98,0.98)) + theme(legend.title=element_blank(), 
                                                         legend.text = element_text(face =  "bold", size = "12"), 
                                                         legend.box.background = element_rect(size=0.7, linetype="solid"),
                                                         legend.justification = c("right", "top"),
                                                         legend.box.margin = margin(1, 1, 1, 1) 
                                                         )
p1
                                          

I tried using legend.key.height legend.spacing.y guide but it only stretched legend keys without adding space between them.我尝试使用legend.key.height legend.spacing.y guide ,但它只拉伸了图例键而没有在它们之间添加空格。 Also how can I remove alternate lables (encircled) of Y-axis keeping tickmark with plot.另外,如何删除带有tickmark的 Y 轴的备用标签(带圆圈)。

在此处输入图像描述

After browsing ggplot2's source code for a bit, I come to the conclusion that the legend.spacing.y is only applied when the byrow = TRUE as argument to the legend.在浏览了 ggplot2 的源代码后,我得出的结论是, legend.spacing.y仅在byrow = TRUE作为图例的参数时才应用。

Simplied example below.下面的简单示例。

library(ggplot2)

ggplot(iris, aes(Sepal.Width)) +
  geom_density(aes(fill = Species)) +
  guides(fill = guide_legend(byrow = TRUE)) +
  theme(legend.spacing.y = unit(1, "cm"))

With regards to the labels, just remove the values from the breaks argument in scale_y_continuous() that you don't want to show, you're already specifying them manually.关于标签,只需从scale_y_continuous()中的breaks参数中删除您不想显示的值,您已经手动指定了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM