0
votes

I have a facet_grid plot with 2 geom_hlines per plot. I'd like to color each of those lines separately. I thought if I added this color to the geom_hline dataframe I could supply the color inside of aes. This colors by group but uses the default ggplot colors.

Here's the code:

p <- qplot(mpg, factor(sample(c("a", "b", "c", "d"), nrow(mtcars), T)), 
           data=mtcars, facets = vs ~ am)

hline.data <- data.frame(z = factor(c("a", "b", "c", "d")), 
    vs = c(0,0,1,1), am = c(0,1,0,1))

hline.data <- transform(hline.data, z0 = as.numeric(z))
hline.data <- rbind.data.frame(hline.data, hline.data)
hline.data[5:8, 1] <- c("b", "c", "d", "a")
hline.data[5:8, 4] <- c(2, 3, 4, 1)
hline.data[, "col"] <- rep(c("red", "black"), each=4)

p + geom_hline(aes(yintercept = z0, colour=col), hline.data)

How can I get the "red" and black geom_hlines I am expecting?

enter image description here

2
Um....scale_colour_manual(values = c("red","black"))? :) Remember, this isn't base graphics!joran
Oh my gosh this is a duh moment, I've been away from ggplot2 too long. Will you post as an answer.Tyler Rinker

2 Answers

4
votes

Since you are specifying the exact values in the data (hline.data), you want to use the identity scale:

+ scale_colour_identity()
2
votes

You just need to set the scale values:

+ scale_colour_manual(values = c("black","red"))