-1

Given the following dataset:

data = cbind(1:10,c('open','reopen','closed'),letters[1:3],1:10)
data = rbind(data,cbind(1:10,c('open','closed','reopen'),letters[1:3],5:10))
data = rbind(data,cbind(1:10,c('closed','open','reopen'),letters[1:3],3:10))
data = data.frame(data);
colnames(data) <- c("id","status","author","when")

I'd like to get a plot similar to the following:

ggplot(data, aes(when,id)) +
  geom_line(aes(group = id,colour = status)) +
  geom_point(aes(group = id,colour = author))

But, as such I get a single legend by 'author' with the status and author values. How can I get the same result but with a legend for author and other for status? My rationale is that I want to layer two plots of the same dataset on top of each other. Plot example

Grasshopper
  • 1,749
  • 1
  • 14
  • 30
  • You would probably be better off using `+geom_point(aes(shape = status))` to use different markers for different status. – Andy Clifton Mar 18 '15 at 21:26
  • I tried that, but shapes are limited to six different values, and the status set is larger. I'm trying to achieve the same effect but using colour. – Grasshopper Mar 18 '15 at 21:33

2 Answers2

0

I don't think you can have different color scales / legends for one ggplot. You could hack something together (see this question for legend hacking), but in this case where one of your geom's is point, you could just use fill and one of the point options that are filled in.

ggplot(data, aes(when,id)) +
  geom_line(aes(group = id,colour = status)) +
  geom_point(aes(group = id, fill = author),
             shape = 21, color = NA, size = 4)

Here the colors used are the same for each, but you can edit the color or fill scales individually, e.g., adding

 scale_fill_brewer(type = "qual") +
 scale_color_brewer(type = "qual", palette = 2)

plot

I do agree with AndyClifton that using color in two ways will be hard to distinguish. You could also experiment with line types, point shapes, or even plotting with geom_text using a word, a letter, or a number as a label instead of points. You say you have more than 6 values for author, but it will be very difficult to distinguish more than 6 colors for author, especially when color is also being used for status.

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks Gregor. I agree the result probably won't be very good with default values ggplot values but I was hoping to work that out tweaking line size and point size. – Grasshopper Mar 18 '15 at 21:43
0

Let's take your data. First you should be aware that you have a problem that your when and id column is a string, so you are plotting 1, 10, 2, 3, ... not 1,...9,10. We can fix that:

data$when.num <-as.numeric(as.character(data$when))
data$id.num <-as.numeric(as.character(data$id))

Then we'll plot it but use different shapes to get two different legends:

require(ggplot2)

p <- ggplot(data, aes(x = when.num, y = id)) +
  geom_line(aes(group = id,colour = status)) +
  geom_point(aes(group = id,shape = author))

print(p)

And you get this:

enter image description here

I think this is much clearer than using coloured points for the author, but this is a question of taste.

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
  • Thanks Andy for the response. The thing is the shape scale is limited to 6 different values, and my author column is larger. I also tried with size but the result wasn`t very pleasing. – Grasshopper Mar 18 '15 at 21:40
  • 1
    it might be worth giving us a more realistic data set to play with? – Andy Clifton Mar 18 '15 at 21:56