【问题标题】:Lost colors and legend after ggplot upgradeggplot升级后丢失颜色和图例
【发布时间】:2016-01-04 23:38:23
【问题描述】:

我真的很喜欢 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% 确定问题出在升级上,可能是我原来的示例出错了。

【问题讨论】:

  • 我只是在玩你的代码,如果你移动 aes() 就会出现图例。尝试:p + geom_abline(aes(intercept=0, slope=1, color="Line1"), linetype="dashed", show.legend=TRUE)
  • 太棒了!那解决了它。你想把它作为一个答案让我接受吗?

标签: r plot ggplot2 legend


【解决方案1】:

我只是在这里重新发表我的评论作为答案。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多