【问题标题】:removing certain values from a ggplot legend created with 2 different geoms从使用 2 个不同几何图形创建的 ggplot 图例中删除某些值
【发布时间】:2020-07-28 18:36:17
【问题描述】:

我制作了一个 ggplot 图,其中的图例是由来自 2 个不同几何图形的映射制成的,如下所示:


# Create data.frame with shading info
shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(mtcars$carb))), by = 1),
                      max = seq(from = 1.5, to = max(as.numeric(as.factor(mtcars$carb))) + 0.5, by = 1),
                      col = c(0,1))

# Plot
ggplot() +
  geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) +
  geom_rect(data = shading,
            aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
                fill = factor(col)), alpha = 0.3) +
  scale_fill_manual(values = c("black", "white", "green", "blue", "red")) +
  geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) 

如何从图例中删除前 2 个图例项(黑色 0 和白色 1)但将其保留在图表上?

谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在scale_fill_manual() 调用中明确设置中断 - 这允许您说出哪些将被标记,哪些不会被标记。 (我在这里保留了剩余的代码)。如果您正在寻找更多信息和演练:https://luisdva.github.io/rstats/set-the-breaks/

    library(ggplot2)
    shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(mtcars$carb))), by = 1),
                          max = seq(from = 1.5, to = max(as.numeric(as.factor(mtcars$carb))) + 0.5, by = 1),
                          col = c(0,1))
    
    # Plot
    ggplot() +
      geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) +
      geom_rect(data = shading,
                aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
                    fill = factor(col)), alpha = 0.3) +
      scale_fill_manual(values = c("black", "white", "green", "blue", "red"),
                        # Adding breaks
                        breaks = c(4, 6, 8)) +
      geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl))) 
    

    reprex package (v0.3.0) 于 2020 年 7 月 28 日创建

    【讨论】:

      【解决方案2】:

      一个非常直接的选择是简单地使用两个比例来填充。

      library(ggplot2)
      library(ggnewscale)
      #> Warning: package 'ggnewscale' was built under R version 4.0.2
      shading <- data.frame(min = seq(from = 0.5, to = max(as.numeric(as.factor(mtcars$carb))), by = 1),
                            max = seq(from = 1.5, to = max(as.numeric(as.factor(mtcars$carb))) + 0.5, by = 1),
                            col = c(0,1))
      
      ggplot() +
        geom_col(data = mtcars, mapping = aes(x = factor(carb), y = mpg, fill = factor(cyl)))+
        scale_fill_manual(values = c( "green", "blue", "red")) +
        new_scale_fill() +
        geom_rect(data = shading,
                  aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf,
                      fill = factor(col)), alpha = 0.3, show.legend = FALSE) +
        scale_fill_manual(values = c("black", "white"))
      

      reprex package (v0.3.0) 于 2020 年 7 月 28 日创建

      【讨论】:

      • 如果外部依赖不成问题,这是一个很好的解决方案。
      猜你喜欢
      • 2021-08-27
      • 2018-10-30
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多