0
votes

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!

1
scale_colour_identity() should work - baptiste
I guess you want something else than what you get if you put the color argument outside of aes(), right? - maj
@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
Would you agree that this question is a duplicate of stackoverflow.com/questions/11511911/… ? If so, please flag it accordingly. - maj
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

1 Answers

1
votes

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