【问题标题】:ggplot2: how to add the legend for a line added to a scatter plot?ggplot2:如何为添加到散点图中的线添加图例?
【发布时间】:2017-08-09 14:11:26
【问题描述】:

我有一些 x 和 y 数据,我想在 ggplot2 散点图上进行比较。 我想添加一条统一线 (y = x)、双重 (y = 2x)、一半 (y = x/2) 和更平滑的线以帮助理解数据,但我找不到如何将这些线添加到情节的传说。有什么想法吗?

set.seed(123)
x <- runif(20, 1, 10)
y <- 0.8 * x + runif(20, -1, 1)
cat <- factor(c(rep("high", 10), rep("low", 10)))
d <- data.frame(x, y, cat)

ggplot(data=d) +
     geom_point(aes(x, y, colour=cat)) +
     geom_abline(aes(intercept=0, slope=1), col = "red") +
     geom_abline(aes(intercept=0, slope=0.5), col="blue", linetype="dotted") +
     geom_abline(aes(intercept=0, slope=2), col="blue", linetype="dashed")+
     geom_smooth(aes(x, y))

y vs x scatter plot in ggplot2

我希望标签“unity line”、“twofold”、“half”和“smoother”出现在图例中的“high”和“low”标签下方。

按照 User3640617 的回答,我尝试了以下代码,但结果仍然不令人满意,因为数据点现在在图例中具有与其链接的线型和平滑线型。

ggplot(data=d) +
     geom_point(aes(x, y, colour=cat)) +
     geom_abline(aes(intercept=0, slope=1, colour="y = x")) +
     geom_abline(aes(intercept=0, slope=.5, colour="y = x/2")) +
     geom_abline(aes(intercept=0, slope=2, colour="y = 2x")) +
     geom_smooth(aes(x,y, colour="smooth")) +
     scale_color_manual(
        values=c("red", "darkgreen", "black", "blue", "red", "blue")) +
     scale_linetype_manual(
        values=c("blank", "blank", "solid", "dashed", "solid", "dotted")) +
     scale_shape_manual(values=c(1, 1, NA, NA, NA, NA))

另外,我似乎无法手动更改 tinetypes:

scatter plot with ablines and smoother

我知道,我可以简单地选择其他颜色,这样会减少混乱,但应该可以有一个图例,只有点代表点,线代表线,而不是点 线代表点和线都行,还是不行?

ggplot2 似乎对在aes 之后添加colorlinetype 感到困扰。将线条添加到图例时,类别的顺序似乎发生了变化。

【问题讨论】:

    标签: r plot ggplot2 legend


    【解决方案1】:

    你可以将一个字符值映射到一个未使用的美学上来欺骗 ggplot 为你制作一个图例:

    ggplot(data=d) +
      geom_point(aes(x, y, colour=cat)) +
      geom_abline(aes(intercept=0, slope=1, lty='unity line'), col = "red")
    

    使用+ scale_linetype(name = NULL)labs(linetype = NULL) 删除linetype 图例标题。

    【讨论】:

    • 非常感谢 Axeman,这对我来说很好用! ...除了我添加了多行并且图例中仅捕获了统一行。对不起,我的错:(我应该提到我有不止一行。我只是想我可以推断出解决方案。我会更新我的问题。
    【解决方案2】:

    我想没有办法绕过包含您的行的第二个数据集。

    获得线条的第二个图例的一种方法是按线型映射线条并硬编码颜色(然后手动调整图例中的颜色)。

    这可以这样完成:

    library(ggplot2)
    
    # 1 recreate the data
    set.seed(123)
    x <- runif(20, 1, 10)
    y <- 0.8 * x + runif(20, -1, 1)
    cat <- factor(c(rep("high", 10), rep("low", 10)))
    d <- data.frame(x, y, cat)
    
    # create the lines-data
    d_lines <- data.frame(int = rep(0, 3),
                          sl = c(1, 0.5, 2),
                          col = c("red", "blue", "blue"),
                          lty = c("solid", "dotted", "dashed"))
    
    # extract the linetypes from the data.frame (with names to map the values correclty)
    ltys <- as.character(d_lines$lty)
    names(ltys) <- as.character(d_lines$lty)
    # extract the colors in the order that ggplot will put the names of the lines (dashed, dotted, solid)
    cols <- as.character(d_lines$col)
    cols <- cols[order(as.character(d_lines$lty))]
    
    # plot the data
    ggplot() +
      geom_point(data = d, aes(x, y, colour=cat)) +
      geom_smooth(data = d, aes(x, y)) +
      # add the two different line-colors
      geom_abline(data = d_lines[d_lines$col == "red", ], 
                  aes(intercept = int, slope = sl, lty = lty), color = "red") +
      geom_abline(data = d_lines[d_lines$col == "blue", ], 
                  aes(intercept = int, slope = sl, lty = lty), color = "blue") +
      # change the color of the legend
      scale_linetype_manual(name = "Linetype", values = ltys,
                            guide = guide_legend(override.aes = list(colour = cols)))
    #> `geom_smooth()` using method = 'loess'
    

    【讨论】:

    • 感谢大卫的解决方案!我正在尝试将平滑线添加到图例中,我真的很惊讶它并不能更轻松地工作(使用标准绘图库似乎更灵活)。
    【解决方案3】:

    也许不是一个非常简洁的解决方案,但您可以创建一个额外的数据框,其中包含您的统一线的信息,如下所示:

    dfabline <- data.frame(intercept=0, slope=1, group="unity line")
    
    ggplot(data=d) +
      geom_point(aes(x, y, colour=cat)) +
      geom_abline(data=dfabline, aes(intercept=intercept, slope=slope,  colour=group))
    

    您可以使用scale_colour_manual 指定线条和点的颜色。

    【讨论】:

    • 感谢您提供这个有用的答案!它适用于多行。似乎没有必要通过额外的数据框,它对我有用,而无需在额外的数据框中定义统一线。一旦我尝试指定线型和/或颜色,似乎 ggplot2 就会受到困扰,它会从图例中删除线。此外,图例显示了一些带有线条的符号,我需要弄清楚如何删除。 (见下面我的回答)
    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 2019-08-22
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多