0

I was looking at The right way to plot multiple y values as separate lines with ggplot2 to get the proper syntax to have multiple Y's for the same X. I was able to do so correctly (in this case I actually used the format of the submitter, as I only wanted one extra Y so it was just as easy).

My trouble comes in that I want to set the two lines to different colors, and I want to specify the color (ie. color = I('blue')), but doing so results in the error

Don't know how to automatically pick scale for object of type AsIs. Defaulting to continuous Error: Discrete value supplied to continuous scale

My code as is follows:

ggplot(aes(x = age), data = by_age) +
  geom_line(aes(y = friend_mean, color = "blue")) +
  geom_line(aes(y = friend_med))

This does result in one line being colored, but not in the color specified, and it applies "blue" as a label.

So how would I go about setting a color in ggplot, using defined, static colors (red, blue, green, etc.)?

Thanks!

Community
  • 1
  • 1
JMichael
  • 569
  • 11
  • 28
  • `scale_colour_identity()` should work – baptiste Aug 27 '15 at 20:27
  • 1
    I guess you want something else than what you get if you put the `color` argument outside of `aes()`, right? – maj Aug 27 '15 at 20:33
  • @maj nope, I just didn't know to put the color designation outside the aesthetic wrapper. That got me what I was looking for. – JMichael Aug 27 '15 at 20:36
  • Would you agree that this question is a duplicate of http://stackoverflow.com/questions/11511911/difference-between-passing-options-in-aes-and-outside-of-it-in-ggplot2 ? If so, please flag it accordingly. – maj Aug 27 '15 at 21:01
  • Close, the one thing the question you linked to doesn't cover is that the color actually applied doesn't match the label (i.e. label says "green", but the line is red, or something like that), which is an important part of my issue. – JMichael Aug 31 '15 at 13:56

1 Answers1

1

All you need to do is to put the color= argument outside of aes(...).

Variables that are supposed to be mapped fit into aes(...) (e.g. a dataframe column named "color_column" that contains three distinct values may be mapped to three different colors when used inside the aesthetics function like aes(color = color_column)), values that are supposed to be static and that you would like to have one defined, fixed value (e.g. color = "red") are located besides aes(...). (This is a broad explanation.)

maj
  • 2,479
  • 1
  • 19
  • 25