change color for two geom_point() in ggplot2

ggplot2, r

Solution

Is this what you are looking for?

ggplot(df, aes(y=id)) +
  geom_point(aes(x=year, color=value1), size=4) +
  geom_point(aes(x=value3, colour ='value3'), size=3) +
  geom_point(aes(x=value2, colour ='value2'), size=5) +
  scale_colour_manual(name="",  
                      values = c("1"="yellow", "2"="orange", "3"="red",
                                 "value3"="grey", "value2"="black"))

Basically, just putting all possible colour labels in a single list.

Problem

Sample dataset: ``` library(ggplot2) df = read.table(text = "id year value1 value2 value3 1 2000 1 2000 2001 1 2001 2 NA NA 1 2002 2 2000 NA 1 2003 2 NA 2003 2 2000 1 2001 2003 2 2002 2 NA 2000 2 2003 3 2002 NA 3 2002 2 2001 NA ", sep = "", header = TRUE) df$value1 <- as.factor(df$value1) ``` I know how to change color for a factor variable with three levels: ``` p <- ggplot(df, aes(y=id)) p <- p + scale_colour_manual(name="", values =c("yellow", "orange", "red")) p <- p + geom_point(aes(x=year, color=value1), size=4) p ``` I can also change the color for two numeric variables: ``` p <- ggplot(df, aes(y=id)) p <- p + scale_colour_manual(name="", values =c("value3"="grey", "value2"="black")) p <- p + geom_point(aes(x=value3, colour ='value3'), size=3) p <- p + geom_point(aes(x=value2, colour ='value2'), size=5) p ``` But I do not know how to change the color for both in the same graph? Does it also work with scale_color_manual? ``` p <- last_plot() + geom_point(aes(x=year, color=value1)) p ```

Original source