Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

r - ggplot2 merge color and fill legends

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...