【问题标题】:Different between colour argument and aes colour in ggplot2?ggplot2中颜色参数和aes颜色之间的区别?
【发布时间】:2014-02-20 01:11:15
【问题描述】:

当我像这样在 aes 中使用颜色时

ggplot(data=olympia,aes(x=year)) + geom_line(aes(y=gold,colour="red")) + geom_line(aes(y=silver,colour="blue"))

它不起作用。

如果我使用颜色参数,它会显示正确的红色和蓝色颜色

ggplot(data=olympia,aes(x=year)) + geom_line(aes(y=gold),colour="red") + geom_line(aes(y=silver),colour="blue")

有什么不同? 有什么问题?

数据框

year gold silver
1 2002   12     16
2 2006   11     12
3 2010   10     13
4 2014    8      3

【问题讨论】:

  • 嗯,第一个拼写为silver,第二个拼写为silber。大概您的olympia 数据集有单词silver 拼写错误为silber(您应该显示数据集以确认这一点)。
  • 您能否显示dput(head(olympia)) 的结果(将其编辑到问题中,而不是放在评论中)
  • 数据集\b年金银\b 1 2002 12 16 2 2006 11 12 3 2010 10 13 4 2014 8 3

标签: r ggplot2


【解决方案1】:

不同之处在于,当您在 aes 中提供颜色参数时,它会将其视为一个因素,并尝试将因素的每个级别映射到一种颜色(与您提供 c("USA", "USA", "Russia", "Russia")-它不会将它们视为文字颜色)。

相反,当您直接将颜色赋予geom_line 时,它会将其作为实际颜色。您可以在geom_line 的文档中看到这一点:

Usage:

       geom_line(mapping = NULL, data = NULL, stat = "identity",
         position = "identity", ...)
<snip>

     ...: other arguments passed on to ‘layer’. This can include
          aesthetics whose values you want to set, not map. See ‘layer’
          for more details.

注意“您要设置其值,而不是映射”。

【讨论】:

  • 问题是直接给geom_line设置颜色不会有图例
猜你喜欢
  • 2018-11-06
  • 2018-12-28
  • 2019-04-26
  • 2020-06-20
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
相关资源
最近更新 更多