升级ggplot后,颜色和图例丢失

3

我非常喜欢ggplot2 2.0改进后的外观,但怀疑升级更改了颜色和图例的定义方式。如何更新我的ggplot 2.0代码?

第一条abline应该是黑色的(仍然是),不应该在图例中。

ablines“Line1”,“Line2”和“Line3”应该有不同的颜色,并且在图例中显示。它们现在都是黑色的。

图例应该可见,但现在不可见。

library(ggplot2)
plot.data <- data.frame(x=c(2, 8), y=c(3, 6))
p <- ggplot(plot.data, aes(x=x, y=y))
p <- p + geom_point(color="black")
p <- p + geom_abline(intercept=0, slope=0.5, color="black", linetype="dashed")
#p <- p + geom_abline(intercept=0, slope=1, aes(color="Line1"), linetype="dashed", show_guide=TRUE)
p <- p + geom_abline(intercept=0, slope=1, aes(color="Line1"), linetype="dashed", show.legend=TRUE)
p <- p + geom_abline(intercept=0, slope=2, aes(color="Line2"), linetype="dashed")
p <- p + geom_abline(intercept=0, slope=3, aes(color="Line3"), linetype="dashed")
p <- p + xlim(0,10)
p <- p + ylim(0,10)
p <- p + theme(legend.title=element_blank(), legend.position="bottom")
p

使用原始代码(如上面的示例中带有#)会收到警告信息“show_guide已被弃用,请改用show.legend”,但是将上面的show_guide更改为show.legend没有任何影响。

注意:我不能100%确定升级是问题所在,可能是我的原始示例有误。


1
我刚刚玩了一下你的代码,如果你移动aes(),图例就会出现。试试这个:p + geom_abline(aes(intercept=0, slope=1, color="Line1"), linetype="dashed", show.legend=TRUE) - MLavoie
太好了!问题解决了。你想把它作为答案,这样我就可以接受它吗? - Chris
1个回答

4

我在这里将我的评论转发为答案。

要使图例重新出现,拦截器和斜率也必须在aes()调用中。

p + geom_abline(aes(intercept=0, slope=1, color="Line1"), linetype="dashed", show.legend=TRUE)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接