4

I want to merge two legends in ggplot2. I use the following code:

ggplot(dat_ribbon, aes(x = x)) +
  geom_ribbon(aes(ymin = ymin, ymax = ymax,
                  group = group, fill = "test4 test5"), alpha = 0.2) +
  geom_line(aes(y = y, color = "Test2"), data = dat_m) +
  scale_colour_manual(values=c("Test2" = "white", "test"="black", "Test3"="red")) +
  scale_fill_manual(values = c("test4 test5"= "dodgerblue4")) +
  theme(legend.title=element_blank(),
        legend.position = c(0.8, 0.85),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_rect(colour = 'purple', size = 0.5)) 

The output is shown below. There are two problems:

  1. When I use two or more words in the fill legend, the alignment becomes wrong
  2. I want to merge the two legends into one, such that the fill legend is just part of a block of 4.

Does anyone know how I can achieve this?

enter image description here

Edit: reproducible data:

dat_m <- read.table(text="x quantile    y   group
1   1   50  0.4967335   0
2   2   50  0.4978249   0
3   3   50  0.5113562   0
4   4   50  0.4977866   0
5   5   50  0.5013287   0
6   6   50  0.4997994   0
7   7   50  0.4961121   0
8   8   50  0.4991302   0
9   9   50  0.4976087   0
10  10  50  0.5011666   0")

dat_ribbon <- read.table(text="
x   ymin    group   ymax
1   1   0.09779713  40  0.8992385
2   2   0.09979283  40  0.8996875
3   3   0.10309222  40  0.9004759
4   4   0.10058433  40  0.8985366
5   5   0.10259125  40  0.9043807
6   6   0.09643109  40  0.9031940
7   7   0.10199870  40  0.9022920
8   8   0.10018253  40  0.8965690
9   9   0.10292754  40  0.9010934
10  10  0.09399359  40  0.9053067
11  1   0.20164694  30  0.7974174
12  2   0.20082056  30  0.7980642
13  3   0.20837821  30  0.8056074
14  4   0.19903399  30  0.7973723
15  5   0.19903322  30  0.8050146
16  6   0.19965049  30  0.8051922
17  7   0.20592719  30  0.8042850
18  8   0.19810139  30  0.7956606
19  9   0.20537392  30  0.8007527
20  10  0.19325158  30  0.8023044
21  1   0.30016463  20  0.6953927
22  2   0.29803646  20  0.6976961
23  3   0.30803808  20  0.7048137
24  4   0.30045448  20  0.6991248
25  5   0.29562249  20  0.7031225
26  6   0.29647060  20  0.7043499
27  7   0.30159103  20  0.6991356
28  8   0.30369025  20  0.6949053
29  9   0.30196483  20  0.6998127
30  10  0.29578036  20  0.7015861
31  1   0.40045725  10  0.5981147
32  2   0.39796299  10  0.5974115
33  3   0.41056038  10  0.6057062
34  4   0.40046287  10  0.5943157
35  5   0.39708008  10  0.6014512
36  6   0.39594129  10  0.6011162
37  7   0.40052411  10  0.5996186
38  8   0.40128517  10  0.5959748
39  9   0.39917658  10  0.6004600
40  10  0.39791453  10  0.5999168")
zx8754
  • 52,746
  • 12
  • 114
  • 209
dreamer
  • 1,192
  • 5
  • 20
  • 42
  • 1
    Can you make this reproducible by including sample data or some code to generate it? – Spacedman May 19 '15 at 09:09
  • For #1 you can space pad them to make names same length. #2 show us [some data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), `dput(dat_ribbon)`. – zx8754 May 19 '15 at 09:12
  • @Spacedman Please see my latest edit. – dreamer May 19 '15 at 09:29
  • @zx8754 Thanks, I included some data in the OP now. – dreamer May 19 '15 at 09:29
  • Can anyone explain me the downvotes? Questions like this usually can be resolved by knowing some command, which is why I thought it wasn't necessary to supply data. And I supplied data when asked. It starts to annoy me how people just downvote without explanation. – dreamer May 19 '15 at 09:48
  • You might want to add `dat_ribbon`, too. Downvotes are because of missing reproducible data. Data helps us to quickly re-run your existing code, and try to find the solution. Help us to help you. – zx8754 May 19 '15 at 10:15
  • 1
    @zx8754 Sorry I forgot. It's been added to the OP now. I understand, I just thought someone might just know the command that's needed to merge legends, which is why I thought posting data seemed unnecessary. Anyway, I'll try to just routinely add it in the future then. – dreamer May 19 '15 at 10:38

1 Answers1

2

You are not using ggplot2 according to its philosophy. That makes things difficult.

ggplot(dat_ribbon, aes(x = x)) +
  geom_ribbon(aes(ymin = ymin, ymax = ymax, group = group, fill = "test4 test5"), 
              alpha = 0.2) +
  geom_line(aes(y = y, color = "Test2"), data = dat_m) +
  geom_blank(data = data.frame(x = rep(5, 4), y = 0.5, 
                               group = c("test4 test5", "Test2", "test", "Test3")), 
             aes(y = y, color = group, fill = group)) +
  scale_color_manual(name = "combined legend",
                     values=c("test4 test5"= NA, "Test2" = "white", 
                              "test"="black", "Test3"="red")) + 
  scale_fill_manual(name = "combined legend",
                    values = c("test4 test5"= "dodgerblue4", 
                               "Test2" = NA, "test"=NA, "Test3"=NA)) 

resulting plot

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thanks a lot for your help! "You are not using ggplot2 according to its philosophy", can you elaborate a bit on this? What exactly do you mean? – dreamer May 19 '15 at 11:12
  • Sorry, but study some ggplot2 tutorials or read Hadley's [paper](http://vita.had.co.nz/papers/layered-grammar.html) . – Roland May 19 '15 at 11:15