uleuchtmann
uleuchtmann

Reputation: 23

line colour in ggplot2

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.

Upvotes: 1

Views: 46

Answers (1)

MrFlick
MrFlick

Reputation: 206232

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

Upvotes: 1

Related Questions