【问题标题】:ggplot2's line legends appear "crossed-out"ggplot2 的线图例出现“划掉”
【发布时间】:2019-01-15 09:35:36
【问题描述】:

我正在创建一个带有两条线的 ggplot,每条线都来自不同的几何图形。举个例子:

df = data.frame(
   x.v = seq(0, 1, 0.025),
   y.v = runif(41)
)
straight.line = data.frame(
   Inter = c(0),
   Slope = c(1)
)

p = ggplot() +
   geom_point(
      mapping = aes(
         x = x.v,
         y = y.v
      ),
      data = df,
      colour = "blue"
   ) +
   geom_smooth(
      mapping = aes(
         x = x.v,
         y = y.v,
         colour = "line of best fit"
      ),
      data = df,
      method = "lm",
      show.legend = NA
   ) +
   geom_abline(
      mapping = aes(
         intercept = Inter,
         slope = Slope,
         colour = "y = x"
      ),
      data = straight.line,
     show.legend = NA
   ) +
   guides(
      fill = "none",
      linetype = "none",
      shape = "none",
      size = "none"
   )

这给出了输出:

如您所见,图例中有奇怪的对角线穿过它。 answer to a similar question 表示可以使用 show.legend = NA 解决此问题。但是,正如您在上面的代码中看到的那样,我这样做并没有改变结果。

有人知道在图例中添加对角线的原因是什么,我还能如何修复它?谢谢。

编辑:关于这是否与this 重复的问题。这可能是答案,但是当链接中的答案使用填充并且我使用颜色时,我该如何应用它?

如果我尝试

+ guides(colour = guide_legend(override.aes = list(colour = NULL)))

我得到了错误

Error in check.length("col") : 'gpar' element 'col' must not be length 0

如果我尝试

+ guides(colour = guide_legend(override.aes = listfill = NULL)))

我得到了错误

Error in `$<-.data.frame`(`*tmp*`, "fill", value = character(0)) : 
  replacement has 0 rows, data has 1

【问题讨论】:

  • 使用show.legend = FALSE 而不是show.legend = NA
  • @Neoromanzer :谢谢,刚刚尝试过,但没有用
  • @kath :这只是阻止了传说的出现
  • 仅在geom_abline 调用中使用show.legend = FALSE。然后颜色图例仍然存在,但没有交叉线。
  • 效果很好,谢谢。

标签: r ggplot2 graph charts legend


【解决方案1】:

以下作品:

library(ggplot2)

ggplot() +
  geom_point(mapping = aes(x = x.v, y = y.v),
             data = df, colour = "blue") +
  geom_smooth(mapping = aes(x = x.v, y = y.v, colour = "line of best fit"),
              data = df, method = "lm", show.legend = NA) +
  geom_abline(mapping = aes(intercept = Inter, slope = Slope, colour = "y = x"),
              data = straight.line, show.legend = FALSE) +
  guides(fill = "none", linetype = "none",  shape = "none", size = "none")

代码可以少一些重复,我们可以省略一些东西(比如guide-call):

ggplot(data = df, mapping = aes(x = x.v, y = y.v)) +
  geom_point(colour = "blue") +
  geom_smooth(aes(colour = "line of best fit"), method = "lm") +
  geom_abline(mapping = aes(intercept = Inter, slope = Slope, colour = "y = x"),
              data = straight.line, show.legend = FALSE) 

为什么我们需要在这里使用show.legend = FALSE 而不是show.legend = NA

来自文档:

show.legend

合乎逻辑。这层应该包含在图例中吗? NA,默认值,包括是否映射了任何美学。 FALSE 从不包含,TRUE 始终包含。也可以是一个命名的逻辑向量来精细选择美学来展示

这意味着我们使用show.legend = NA 来调用geom_abline——我们在图例中使用了这一层。但是,我们不想使用这一层,因此需要show.legend = FALSE。您可以看到这不会影响图例中包含哪些颜色,仅影响图层。

数据

set.seed(42) # For reproducibilty
df = data.frame(x.v = seq(0, 1, 0.025),
                y.v = runif(41))
straight.line = data.frame(Inter = 0, Slope = 1)

【讨论】:

    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2018-07-23
    • 2018-05-11
    相关资源
    最近更新 更多