0

I have plotted positive values and negative values exactly as I would like them to be, legend included. I am now putting these together in one plot to create a single plot (I do not have enough points to link to this image). I am very pleased with this - except for the legend. The legend is sorted alphabetically which is not the my preferred order (the same order as the colour layers from top to bottom). I have tried different ways of trying to manually set the legend but all I have found so far works when plotting a single data frame and not two as I am.

It seems that using scale_colour_manual does not affect this type of plot, is there another way of doing this?

Below is a very simple working example that also gives the same behaviour described above.

data1 = data.frame(x = seq(0,20), y =    c(1,4,-5,-9,-4,0,3,6,-4,3,-2,-5,8,2,1,6,7,-9,8,-4,0))
data2 = data.frame(x = seq(0,20), y = seq(0,20))

q = ggplot(NULL, aes(x = x, y = y))
q = q + geom_line(data = data1)
q = q + geom_line(data = data2)

Thanks, Jane


Thanks for the reply but I am still not getting the graph that I would like. Here is a link to closest that I have come to so far positive and negative values - legend not as required. For this plot I have used two separate data frames as I am using different geom_area and geom_line positions; for the positive part of the plot I am using:

geom_area(data = prod.data, position = "identity", alpha = 0.8)
geom_line(data = prod.data, position = "identity")

and for the negative part of the plot I am using:

geom_area(data = cons.data, position = "stack", alpha = 0.8)
geom_line(data = cons.data, position = "stack")

However, as discussed in the original question I seem to have no control over the order of the legend. I tried combining the data frames and set the order of the factor, here I used:

geom_area(position = "stack", alpha = 0.8)
geom_line(position = "stack")

and I have control over the legend order but the actual plot is not as I would like it. Here is the graph positive and negative values - graph not as required.

I'm having quantum mechanics flashbacks :-) so any further assistance is greatly appreciated.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Ganna
  • 1
  • Try setting the levels of `Reaction` column like `data$Reaction = factor(data$Reaction, levels=c("C", "A", "B", "D"), ordered=T)` where `C,A,B,D` is the desired order in your legend... – Arun Feb 14 '13 at 17:06
  • And btw, this plot of yours for the dummy data you've shown does not produce any legend. – Arun Feb 14 '13 at 17:07

1 Answers1

1

Generally, ggplot works best when you combine your data into one data frame. Your provided example is a bit confusing--as written there is not a legend, and it's not clear what you'd want the legend to be. The way I'd go about it would be to combine the data frames:

data1$group <- 1
data2$group <- 2
data3 <- rbind(data1, data2)
data3$group  <- factor(data3$group)
q <- ggplot(data3, aes(x = x, y = y, group = group, color = group))
    + geom_line()

You can then use any of the standard ways to order the legend. Less automatic, but often superior to a normal legend, you could use a new data.frame and a geom_text call to manually label each line.

The legend will, by default, be sorted in the order of the factor, so to reorder the legend, you should order the factor, either manually using factor

data3$group <- reorder(data3$group, levels = c(2, 1), ordered = TRUE)

or using reorder and a function (such as sort(), or rev(sort()), if you want reverse alpha order). You could also use reorder with a function of a different column if you wanted to order the factor by mean or max y value, for example.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294