【问题标题】:How to combine fill (columns) and color (points and lines) legends in a single plot made with ggplot2?如何在使用 ggplot2 制作的单个图中组合填充(列)和颜色(点和线)图例?
【发布时间】:2021-09-11 18:05:30
【问题描述】:

在图表中,我有列和点。我正在尝试统一传说;我已经在秤上放了相同的名字,但它们仍然是分开的。有人可以帮我解决这个问题吗?

library(ggplot2)

X <- factor(c("a", "b"))
Y1 <- c(10, 15)
Y2 <- c(22, 23)

df <- data.frame(X, Y1, Y2)

ggplot(data = df, aes(x = X,
                      group = 1)) +
  geom_col(aes(y = Y1,
               fill = "Y1")) +
  geom_line(aes(y = Y2,
                color = "Y2")) +
  geom_point(aes(y = Y2,
                 color = "Y2")) +
  scale_fill_manual(name = "Legend",
                    values = "blue") +
  scale_color_manual(name = "Legend",
                     values = "red")

【问题讨论】:

    标签: r ggplot2 scale legend legend-properties


    【解决方案1】:

    要合并图例,您还必须在填充和色标中使用相同的值。此外,您必须通过使用guide_legendoverride.aes 参数删除“Y2”图例键的填充颜色来稍微调整图例:

    编辑感谢@aosmith 的评论。为了使这种方法适用于 ggplot2 版本 limits。

    library(ggplot2)
    
    X <- factor(c("a", "b"))
    Y1 <- c(10, 15)
    Y2 <- c(22, 23)
    
    df <- data.frame(X, Y1, Y2)
    
    ggplot(data = df, aes(x = X,
                          group = 1)) +
      geom_col(aes(y = Y1,
                   fill = "Y1")) +
      geom_line(aes(y = Y2,
                    color = "Y2")) +
      geom_point(aes(y = Y2,
                     color = "Y2")) +
      scale_fill_manual(name = "Legend",
                        values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
      scale_color_manual(name = "Legend",
                         values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
      guides(color = guide_legend(override.aes = list(fill = c("blue", NA))))
    

    【讨论】:

    • 我无法重现此代码。出现以下错误信息: [[(*tmp*`, i, value = c("blue", NA)) 中的错误:replacement has 2 rows, data has 1
    • 嗯。诡异的。我刚刚通过 reprex 包再次运行了代码。它工作正常。也许 ggplot2 版本有问题?我正在使用 ggplot2 3.3.5。
    • 我的版本是 4.0.5。有没有办法在 4.0.5 版中做到这一点?
    • 仅供参考,基于this issue that has been marked as a bug,我不肯定这种方法将来会一直有效,而无需在每个比例中明确设置limits
    • @DanielValenciaC。检查您的 ggplot2 版本,即packageVersion("ggplot")。 4.0.5 是 R 版本。
    【解决方案2】:

    另一种选择是只制作一个图例,然后手动更改该图例以使其代表所有层。

    在这种情况下,我创建了一个 fill 图例,但将 color 设置为 aes() 之外的常量。我通过添加show.legend = TRUE 来强制包含线/点图层。

    然后我将额外的值添加到fill,在scale_fill_manual() 中设置valueslimits。注意我将Y2 设置为透明填充。

    最后,我对图例进行了手动更改,以仅在 guide_legend() 中使用 override.aes 的第二个图例键保留线条和点。

    ggplot(data = df, aes(x = X,
                          group = 1)) +
        geom_col(aes(y = Y1,
                     fill = "Y1")) +
        geom_line(aes(y = Y2), color = "red", show.legend = TRUE) +
        geom_point(aes(y = Y2), color = "red", show.legend = TRUE) +
        scale_fill_manual(name = "Legend",
                          values = c(Y1 = "blue", Y2 = "transparent"),
                          limits = c("Y1", "Y2")) +
        guides(fill = guide_legend(override.aes = list(shape = c(NA, 19),
                                                       linetype = c(0, 1))))
    

    reprex package (v2.0.0) 于 2021 年 6 月 29 日创建

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多