0

I am trying to vary the color of the LINE in an R graph (within the same series of data). For example if you had points showing temperature for weeks of the year, and they're connected with geom_line() or equivalent, how would I show say the line being deeper red for higher temperature weeks and gradually changing to say yellow in colder weeks? (if BOTH points and line could vary across the same palette/gradient based on the same variable - say temperature, that would be ideal).

bp.df <- NULL
bp.weeks <- 26
bp.days <- 7 * bp.weeks
bp.df$day <- 1:bp.days
bp.df$week <- ceiling(bp.df$day / 7)
bp.mean.normal <- 100
bp.sd.normal <- 20

bp.df <- as.data.frame(bp.df)

bp.df$normal <- rnorm(nrow(bp.df),bp.mean.normal, bp.sd.normal)
bp.df$day <- as.numeric(bp.df$day)

# make sure ggplot2 is installed and loaded

g <- ggplot(bp.df, aes(x = day, y = normal, color = normal)) + 
    geom_point() + 
    geom_line(col = bp.df$normal)
g

Why do the line colors not match the levels of the "normal" variable? I understand that, since they connect two dots, the lines must "decide" which one's value to use, but this output seems to make the colors completely random.

If varying across a gradient won't work, how would I make the line be red for the first 50 days, green for the next 50, etc?

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 1
    It _does_ match; it uses the value of `normal` for the left-most point of the segment, because that's the vector you're passing via `aes`. To make color vary for each 50 days, add a variable to pass as the color aesthetic, e.g. `ggplot(bp.df, aes(x=day, y = normal, color = cut(day, seq(0, 200, 50)))) + geom_point() + geom_line()` – alistaire Dec 16 '16 at 00:53
  • Thank you, this and the next comment definitely clarified my confusion. Clearly a separate variable makes sense to drive the colors. – Robert Ribciuc Dec 16 '16 at 03:01

1 Answers1

1

Try this:

g <- ggplot(bp.df, aes(x=day, y = normal, color = normal)) + geom_point() +
geom_line(aes(color=normal))

g

It seems the col argument is different than the color in the aes argument.

thc
  • 9,527
  • 1
  • 24
  • 39
  • Per [this post](http://stackoverflow.com/questions/32543340/behavior-ggplot2-aes-in-combination-with-facet-grid-when-passing-variable-wi), using `$` in `aes` can cause unexpected results. You can modify this code to `g <- ggplot(bp.df, aes(x=day, y = normal, color = normal)) + geom_point() + geom_line()` and get the same result. Since you already have `color = normal` in your original `aes` argument in `ggplot`, you don't need to call it again. – Nick Criswell Dec 16 '16 at 01:02
  • Thanks, I didn't realize that. It sounds like a bug which hopefully will be fixed. – thc Dec 16 '16 at 02:45
  • Thanks guys, very helpful. – Robert Ribciuc Dec 16 '16 at 03:02
  • @thc, this is not a bug in `ggplot`. Since `ggplot` takes in data as a `data.frame`, there should be no need to reference the column names within an `aes` argument using the `$`. Also, in your modified answer, you do not need the `aes(color=normal)` within the `geom_line` call. You already set up the `colour` aesthetic in your original call to `ggplot`. I don't think this will affect anything, but it is a redundant call. – Nick Criswell Dec 16 '16 at 14:39