【发布时间】: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