【问题标题】:How to put boxes around alpha values in legend for ggplot2如何在ggplot2的图例中围绕alpha值放置框
【发布时间】:2021-02-02 04:59:00
【问题描述】:

我想在图例中的 alpha 值周围放置黑框,以便实际显示最小值 (70)。需要说明的是:围绕代表每个 alpha 值的每个单独的正方形,而不是围绕整个图例。

mtcars
test_plot <- ggplot() +
  geom_point(data = mtcars, 
             aes(x = wt, y = mpg,
                 alpha = disp,
                 size = hp), 
             fill = 'black',
             shape = 21) + 
  geom_point(data = mtcars, 
             aes(x = cyl, y = mpg,
                 size = hp), 
             fill = 'black',
             shape = 1) +
  xlab("Weight") + ylab("MPG") + 
  labs(size = "Horsepower:", alpha = "Displacement:") +
  scale_size_continuous(range = c(1,10), breaks= c(50, 100, 150, 200, 250, 300, 350)) +
  scale_alpha_continuous(range = c(0,1), 
                         breaks= c(71.1, 100, 200, 300, 400, 500), 
                         labels = c(70, 100, 200, 300, 400, 500)) +
  guides(size = guide_legend(override.aes = list(shape = 1)),
         alpha = guide_legend(override.aes = list(shape = 22, color = 'black')))

test_plot

Example of the plot

【问题讨论】:

    标签: r ggplot2 legend alpha


    【解决方案1】:

    问题在于alpha 值同时应用于图例键的fill 和边框color。不幸的是,我不知道有一个简单的解决方案。获得所需结果的一种方法是操作gtable

    library(ggplot2)
    
    p <- ggplot() +
      geom_point(data = mtcars, 
                 aes(x = wt, y = mpg,
                     alpha = disp,
                     size = hp), 
                 fill = 'black',
                 shape = 21) + 
      geom_point(data = mtcars, 
                 aes(x = cyl, y = mpg,
                     size = hp), 
                 fill = 'black',
                 shape = 1) +
      xlab("Weight") + ylab("MPG") + 
      labs(size = "Horsepower:", alpha = "Displacement:") +
      scale_size_continuous(range = c(1,10), breaks= c(50, 100, 150, 200, 250, 300, 350)) +
      scale_alpha_continuous(range = c(0,1), 
                             breaks= c(71.1, 100, 200, 300, 400, 500), 
                             labels = c(70, 100, 200, 300, 400, 500)) +
      guides(size = guide_legend(override.aes = list(shape = 1)),
             alpha = guide_legend(override.aes = list(shape = 22, color = 'black')))
    
    library(gtable)
    library(grid)
    
    g <- ggplotGrob(p)
    for (i in c(4, 6, 8, 10, 12)) { # Indices of legend keys
      # guide-box is element 15 in the gtable
      # alpha legend if the first element of the "guide-box"
      gp <- g$grobs[[15]]$grobs[[1]]$grobs[[i]]$gp
      gp <- modifyList(gp, list(col = "black"))
      
      g$grobs[[15]]$grobs[[1]]$grobs[[i]]$gp <- do.call(grid::gpar, gp)
    }
    grid.newpage()
    grid.draw(g)
    

    【讨论】:

      【解决方案2】:

      将变量映射到fill 而不是alpha,并使用具有边缘的某些形状,并使用具有不同alpha 值的scale_fill_gradient*。使用stroke控制边框的大小。

      我简化了您的示例以突出代码的相关点:

      ggplot(mtcars, aes(wt, mpg)) +
        geom_point(aes(fill = disp), shape = 21, size = 10, stroke = 1e-10) +
        scale_fill_gradient(low = alpha("black", 0), high = alpha("black", 1),  guide = "legend") +
        guides(fill = guide_legend(override.aes = list(size = 6, stroke = .5)))
      

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 1970-01-01
        • 2012-09-18
        • 2021-11-29
        • 2018-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多