ggplot2合并颜色和填充图例

时间:2015-05-19 09:04:03

标签: r ggplot2

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

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

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

  1. 当我在填充图例中使用两个或多个单词时,对齐方式会出错
  2. 我想将两个图例合并为一个,这样填充图例只是一个4块的一部分。
  3. 有谁知道我怎么做到这一点?

    enter image description here

    修改:可重现的数据:

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

1 个答案:

答案 0 :(得分:2)

根据其理念,您没有使用ggplot2。这让事情变得困难。

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