ggplot2合并颜色和填充图例 [英] ggplot2 merge color and fill legends

查看:1252
本文介绍了ggplot2合并颜色和填充图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并ggplot2中的两个图例。我使用下面的代码:

$ pre $ g $ p $ 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( (),
theme(legend.title = element_blank(),
legend.position = c(0.8,0.85),
legend.background = element_rect(fill =transparent),
legend.key = element_rect(color ='purple',size = 0.5))

输出如下所示。有两个问题:


  1. 当我在填充图例中使用两个或多个单词时,对齐方式变为错误
  2. 我想将两个图例合并为一个,这样填充图例只是4块的一部分。

有人知道我可以做到这一点吗?



编辑:重复性数据:

  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.0997 9283 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)


解决方案
  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))


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?

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

解决方案

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

这篇关于ggplot2合并颜色和填充图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆