【问题标题】:Position a ggplot Legend label directly over its fill colour将 ggplot 图例标签直接放置在其填充颜色上
【发布时间】:2023-03-12 17:40:01
【问题描述】:

对于 ggplot 图,我希望将图例的值(此处为 0 和 1)定位在它们所代表的颜色之上,而不是它们的一侧。我的意思不是在彩色正方形的左侧、右侧、上方或下方,而是在正方形本身内。这将导致红色正方形内的数字 0 和蓝色正方形内的数字 1。如何做到这一点?

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
geom_bar() +
theme(legend.position = "top", 
    legend.direction = "horizontal") +
guides(color = guide_legend(title.position = "left", label.position = "top")) 

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    由于您使用的是fill,因此您需要在guides 中使用填充,然后使用label.vjusttitle.vjust 来排列所有内容。

    library(tidyverse)
    
    ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
      geom_bar() +
      theme(legend.position = "top", 
            legend.direction = "horizontal") +
      guides(fill = guide_legend(label.vjust = -7, label.position = "top", title.vjust = 0.2))
    

    reprex package (v0.2.1) 于 2018 年 11 月 23 日创建

    【讨论】:

      【解决方案2】:

      稍作调整,您可以使用change-geom-texts-default-a-legend-to-label-string-itself 所示的方法。这改变了键生成函数,因此允许ggplot2 计算位置来为您放置标签。

      我们的想法是为 geom_bar 图例键保留原始 rectGrob,并使用额外的 textGrob 将标签放在顶部。

      library(ggplot2)
      library(grid)
      
      oldK <- GeomBar$draw_key # to save for later
      
      # see other answer on how to tweak function based n manual colours
      GeomBar$draw_key <- function (data, params, size, 
                                    var=unique(mtcars$vs),
                                    cols=scales::hue_pal()(length(var))) {
      
          # match labels to the fill colour
          txt <- if(is.factor(var)) levels(var) else sort(var)
          txt <- txt[match(data$fill, cols)]
      
          # This is from the original GeomBar$draw_key -------------------
          lwd <- min(data$size, min(size)/4)
          rg <- rectGrob(width = unit(1, "npc") - unit(lwd, "mm"), height = unit(1, 
              "npc") - unit(lwd, "mm"), gp = gpar(col = data$colour, 
              fill = alpha(data$fill, data$alpha), lty = data$linetype, 
              lwd = lwd * .pt, linejoin = "mitre"))
          # ---------------------------------------------------------------
      
          # Add text label: lifted from other answer
          # hard-coded text size multiplier -- got to be a better way!
          tg <- textGrob(txt, 0.5, 0.5,  
                   just="center", 
                   gp = gpar(col = "black", 
                             fontfamily = data$family, 
                             fontface = data$fontface, 
                             fontsize = 10*data$size * .pt)) 
          # ---------------------------------------------------------------
      
          grobTree(rg, tg) # output new key
      }
      

      然后你可以绘制

      ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
        geom_bar() +
        theme(
          legend.position = "top", 
          legend.direction = "horizontal",
          legend.text = element_blank())
      
      # reset key function to original
      GeomBar$draw_key <- oldK
      

      这对于调整绘图大小应该相当可靠。

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2022-11-22
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        • 2017-02-05
        相关资源
        最近更新 更多