Gerhard
Gerhard

Reputation: 111

Different between colour argument and aes colour in ggplot2?

When I use the colour in aes like this

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

it does not work.

If I use the colour argument it shows the right colours red and blue

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

What is the different? What's the fault?

Dataframe

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

Upvotes: 2

Views: 2287

Answers (1)

David Robinson
David Robinson

Reputation: 78590

The difference is that when you provide the colour argument in aes, it treats it as a factor, and tries to map each level of the factor to a color (the same way it would if you gave c("USA", "USA", "Russia", "Russia")- it wouldn't treat them as literal colors).

In contrast, when you give the colour directly to geom_line, it takes it as an actual color. You can see this in the documentation for 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.

Notice "whose values you want to set, not map".

Upvotes: 5

Related Questions