1

I don't understand the following: Why does

data_ts <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) + seq(-140, 224)^2 / 10000)
ggplot(data_ts, aes(x=day, y=value)) +
  geom_line() +
  scale_colour_manual(values = "#ffcc33")

produce a black line? I know, I could use

ggplot(data_ts, aes(x=day, y=value)) +
  geom_line(colour = "#ffcc33")

instead, but I'd like to understand why 'scale_colour_manual' does not work in the example above.

1 Answers1

1

The scale_colour_manual function only effects values that are mapped via an aesthetic aes(). Same goes for all scale_* functions. If values aren't set inside the aes(), then the scale won't touch them. If you wanted to use scale_colour_manual, it would need a mapping. Something like

ggplot(data_ts, aes(x=day, y=value)) +
  geom_line(aes(color="mycolor")) +
  scale_colour_manual(values = "#ffcc33")

or to ensure a correct match up between mapped literal values and colors, you can do something like

ggplot(data_ts, aes(x=day, y=value)) +
  geom_line(aes(color="mycolor1")) +
  geom_line(aes(y=value+1, color="mycolor2")) +
  scale_colour_manual(values = c(mycolor1="#ffcc33", mycolor2="#33ccff"))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Great. Now I understand. Thank you vm. Which leads me to the question of how to change the standard line color. Even '+theme(line = element_line(colour = "#ffcc33"))' does not do the trick. Where does ggplot2 set the standard color of a line to "black"? – uleuchtmann Dec 21 '20 at 08:39
  • I think this post would answer that question: https://stackoverflow.com/questions/21174625/ggplot-how-to-set-default-color-for-all-geoms – MrFlick Dec 21 '20 at 08:40
  • Wow! Just what I needed. Thank u vm!! – uleuchtmann Dec 21 '20 at 08:42