ggplot2:顶部图例键符号大小随图例键标签而变化

Chr*_*ion 5 r legend ggplot2

问题

我想将情节的图例放在情节上方。我还想在图例键标签(图例文本)上方放置图例键符号(彩色正方形)。不幸的是,当我这样做时,图例键符号会“拉伸”以适合标签的大小。我认为ggplot2可以正常工作,但是如何手动覆盖此功能?

如何在顶部使用可变长度的标签使图例键符号保持一致?

可重现的例子

这不一定是一个最小的示例,以防万一我的实际代码的结构(如coord_flipfill调用有影响))

library(dplyr)
library(ggplot2)

dataFrame <- diamonds %>%
              group_by(color, cut) %>%
              summarise(count = n()) %>%
              group_by(color) %>%
              mutate(percent = count/sum(count),
                    pretty_label = paste0(round(percent*100, 1), "%")) %>%
              ungroup()

p <- ggplot(data = dataFrame, mapping = aes(x=color, y = percent, group = cut))+
      geom_bar(aes(fill = cut), stat = "identity", position = "fill")+
      geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5), colour="white", stat = "identity")+
      coord_flip()+
      theme(legend.position="top")+
      guides(fill = guide_legend(label.position = "bottom", reverse = TRUE))

plot(p)
Run Code Online (Sandbox Code Playgroud)

图例符号带有标签

请注意,每个图例符号的大小如何不同,具体取决于标签的长度。

我已经尝试过的

我想象它与指南有关,但是我似乎无法正确地做到这一点。使用(p)上方的图,我尝试了以下操作:

  1. 这里这里p + guides(colour = guide_legend(override.aes = list(size=3)))

  2. 这里p + guides(colour = guide_legend(keywidth = .5, keyheight = .5))p + guides(colour = guide_legend(keywidth = unit(.5, "cm"), keyheight = unit(.5, "cm")))

  3. 这里开始:(尝试包装标签)p + guides(color = guide_legend(nrow = 2))

我已经尝试了其他更少的“逻辑”尝试,这仅仅是因为。没有一个工作。

最后的想法

我可能很难知道要搜索什么。如果您能够指出正确的方向,我总是愿意亲自解决问题。任何其他资源都值得欢迎。

提前致谢!

会话输出

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2.2 ggplot2_3.0.0  dplyr_0.7.6   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18      bindr_0.1.1       magrittr_1.5      tidyselect_0.2.4  munsell_0.5.0     colorspace_1.3-2  viridisLite_0.3.0
 [8] R6_2.2.2          rlang_0.2.1       plyr_1.8.4        tools_3.5.1       grid_3.5.1        gtable_0.2.0      withr_2.1.2      
[15] yaml_2.1.19       lazyeval_0.2.1    assertthat_0.2.0  digest_0.6.17     tibble_1.4.2      purrr_0.2.5       glue_1.2.0       
[22] labeling_0.3      compiler_3.5.1    pillar_1.2.3      scales_0.5.0      pkgconfig_2.0.1  
Run Code Online (Sandbox Code Playgroud)

eip*_*i10 5

我不知道是否有一种方法可以独立于文本来控制图例颜色框的宽度(除了破解图例杂项之外)。但是,如果legend.key.width=unit(1.5, "cm")在主题声明中添加,则所有颜色框都将扩展为相同的宽度(您可能需要稍微向上或向下调整1.5以获得所需的框宽)。

library(scales)

ggplot(dataFrame, aes(x=color, y = percent))+
  geom_bar(aes(fill = cut), stat = "identity") +
  geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5), 
            colour="white", size=3)+
  coord_flip()+
  theme(legend.position="top",
        legend.key.width=unit(1.5, "cm"))+
  guides(fill = guide_legend(label.position = "bottom", reverse = TRUE)) +
  scale_y_continuous(labels=percent)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

您可以通过添加Very Good两行来节省一些空间:

library(forcats)

ggplot(dataFrame, aes(x=color, y = percent, fill = fct_recode(cut, "Very\nGood"="Very Good")))+
  geom_bar(stat = "identity") +
  geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5), 
            colour="white", size=3)+
  coord_flip()+
  theme(legend.position="top",
        legend.key.width=unit(1.2, "cm"))+
  guides(fill = guide_legend(label.position = "bottom", reverse = TRUE)) +
  labs(fill="Cut") +
  scale_y_continuous(labels=percent)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明