2

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
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Gerhard
  • 111
  • 1
  • 1
  • 7
  • Well, it's spelled `silver` in the first and `silber` in the second. Presumably your `olympia` dataset has the word `silver` misspelled as `silber` (you should have shown the dataset to confirm this). – David Robinson Feb 19 '14 at 16:53
  • 2
    Can you please show the result of `dput(head(olympia))` (edit it into the question rather than putting it in a comment) – David Robinson Feb 19 '14 at 17:01
  • Dataset \b year gold silver \b 1 2002 12 16 2 2006 11 12 3 2010 10 13 4 2014 8 3 – Gerhard Feb 19 '14 at 17:01

1 Answers1

5

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".

David Robinson
  • 77,383
  • 16
  • 167
  • 187