14

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
Jules
  • 143
  • 1
  • 1
  • 4
  • 1
    Could you use the reshape package to melt the columns together? This seems close but not right... require(reshape) melt(df,id.vars = c("id","year"), measure.vars=c("value1","value2")) df$value <- as.factor(df$value) ... – colcarroll Nov 04 '13 at 23:14
  • probably, but I am looking for a solution that also works for more than two geom_points() and preferably directly in ggplot2. I thought that there exists a simple method within ggplot2 but I am still desperately looking for it.. – Jules Nov 05 '13 at 08:30

2 Answers2

28

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

enter image description here

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

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
2

JLLagrange was on to the right idea. Use melt from reshape2 to convert your data to long form before plotting.

df_long <- melt(df,id.vars = c("id", "year"), measure.vars=c("value2", "value3"))
(p <- ggplot(df_long, aes(y = id)) + 
  scale_colour_manual(name = "", values = c(value3 = "grey", value2 = "black")) +
  scale_size_manual(name = "", values = c(value3 = 3, value2 = 5)) +
  geom_point(aes(x = value, colour = variable, size = variable))
)

In light of your comment, your data should be in a different form. Essentially, you are considering value2 and value3 to be the same as year, but with additional levels for value1. Reconstruct your data like this:

df1 <- df[, c("id", "year", "value1")]

df2 <- data.frame(
  id     = df$id,
  year   = df$value2,
  value1 = "4"
)

df3 <- data.frame(
  id     = df$id,
  year   = df$value3,
  value1 = "5"
)

df_all <- rbind(df1, df2, df3)
df_all$value1 <- factor(df_all$value1)

Then you can draw a plot with this:

(p <- ggplot(df_all, aes(id, year, colour = value1)) + 
  geom_point(
    size = 3, 
    position = position_jitter(height = 0, width = 0.05)
  ) +
  scale_colour_manual(
    values = c("1" = "yellow", "2" = "orange", "3" = "red", "4" = "grey", "5" = "black")
  )
)

(I've added a bit of jitter to the points so you can see where they overlap. You could also set an alpha value in geom_point.)

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • I do not understand. Your example just gives me the plot for value2 and value3 in grey and black but it does not include value1, does it? – Jules Nov 05 '13 at 21:16
  • @Jules Your question reads as if you want to change the colours for value2 and value3 in the second plot. You haven't drawn a plot with all three values in it. If my solution isn't what you want, then please clarify the question. – Richie Cotton Nov 07 '13 at 10:00
  • : basically in a first step, I want to change the color of value1 to yellow ,orange and red (or a continous color scale, too make it more flexibel if I had more than 3 levels) . In a second step I want to add value2 and value3 (in grey and black) to the graph created in the first step so I end up with one graph including all three values. – Jules Nov 08 '13 at 10:22