【问题标题】:ggplot2: Legend for labeling multiple geom_abline slopesggplot2:标记多个 geom_abline 斜坡的图例
【发布时间】:2017-08-05 16:57:29
【问题描述】:

我正在使用data.frame(下面有dput)来使用geom_abline 绘制三个斜坡:

  moderator_value simple_intercept simple_slope
1              -1      -0.02745523    0.2768973
2               0       0.05990693    0.2147829
3               1       0.14726909    0.1526684

我现在拥有的是这段代码:

ggplot() +
  geom_abline(data=ablines, 
              mapping=aes(slope=simple_slope, intercept=simple_intercept),
              linetype=c(1,2,3)) +
  scale_x_continuous(limits=c(-1.5,2), name="Prejudice") +
  scale_y_continuous(limits=c(-.75, .75), name="Authenticity") +
  theme_light() +
  theme(text=element_text(size=14))

这会返回图形:

我想添加一个图例,用它们的linetype 标记这三个单独的行。我在其他地方查看过 SO,但其中许多说只是将 show_guide 包含在 geom_abline 函数中(现在已弃用以支持 show.legend)并将其设置为 TRUE。这对我不起作用。我也尝试过使用scale_linetype_manual,但没有成功。

如何添加单独标记每条线的图例?我想包含主持人变量的名称以及“-1 SD”、“平均值”和“+1 SD”作为标签。

dputablines 数据:

structure(list(moderator_value = c(-1, 0, 1), simple_intercept = c(-0.0274552251655293, 
0.0599069333124192, 0.147269091790368), simple_slope = c(0.276897278474258, 
0.214782863579552, 0.152668448684846)), .Names = c("moderator_value", 
"simple_intercept", "simple_slope"), row.names = c(NA, 3L), class = "data.frame")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您应该尝试将每条线的独特特征(即,将调节器作为一个因素(b.c. 我们不想将其解释为连续变量))映射到线型。

    例如通过使用此代码

    ablines <- structure(list(moderator_value = c(-1, 0, 1), 
                              simple_intercept = c(-0.0274552251655293, 0.0599069333124192, 0.147269091790368), 
                              simple_slope = c(0.276897278474258, 0.214782863579552, 0.152668448684846)),
                         .Names = c("moderator_value", "simple_intercept", "simple_slope"), 
                         row.names = c(NA, 3L), class = "data.frame")
    
    library(ggplot2)
    ggplot(ablines) +
     geom_abline(mapping = aes(slope = simple_slope,
                               intercept = simple_intercept, 
                               linetype = as.factor(moderator_value))) +
     scale_x_continuous(limits=c(-1.5,2), name="Prejudice") +
     scale_y_continuous(limits=c(-.75, .75), name="Authenticity") 
    

    【讨论】:

      【解决方案2】:

      要获得图例,您必须在 aes() 中将变量映射到 lynetipe。在您的代码中,您在 aes() 之外指定了它。 请注意,在我的代码中,变量“moderator”的数值会将该数字映射到 ggplot 的可用线条样式。 要为每个线型指定自定义名称,请取消注释最后一条指令。

      ggplot() +
            geom_abline(data=ablines, 
                        mapping=aes(slope=simple_slope, intercept=simple_intercept, linetype = moderator_value)) +
            scale_x_continuous(limits=c(-1.5,2), name="Prejudice") +
            scale_y_continuous(limits=c(-.75, .75), name="Authenticity") +
            theme_light() ## +
            ## scale_linetype_continuous(labels = c("First Line", "Second Line", "Third Line")
      

      【讨论】:

        猜你喜欢
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 2020-04-20
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多