ggplot-使用图例在同一图上绘制线和点

时间:2018-07-19 12:00:36

标签: r ggplot2 legend confidence-interval

我有一个熔化的数据框,叫做dat。它包括4条线和1点数据。 我正在尝试给每条线(和点)赋予颜色和置信区间(geom_ribbon),并创建一个图例。

  cols <- c(rgb(0.107, 0.396, 0.592),rgb(0.107, 0.696, 0.392),rgb(0.422,0.422,0.422))
  p1 <- ggplot(dat,aes(x=Tvec,y=Floods,color=L1,linetype=factor(lines),shape=factor(points)))+
    geom_ribbon(aes(ymin = CIlow90,ymax = CIhigh90,fill=L1), colour=NA,alpha = 0.4) +
    geom_line(size=0.8)+geom_point()+
    scale_x_continuous(trans = 'log10',limits=c(1,5)) +
    ylim(0.1,2*min(c(max(dfplot$Floods),max(dfpp1$Floods)))) +
    scale_color_manual(labels = c("GEV-MLE", "GEV-PWM","Empirical Dist."),
                       values=cols,name = 'Distributions') +
    scale_fill_manual(name = '',  values=cols,labels='')+
    guides(shape = FALSE, linetype = FALSE,
           colour = guide_legend(override.aes =  list(shape = c(NA,NA,16),
                                                      linetype = c("solid","solid","blank"))))
  p1

这是我得到的: enter image description here

这是“日期”数据框:

    structure(list(Floods = c(6.3, 4.8, 4.56, 4.44, 4, 3.78, 3.3, 
3.02, 2.16, 1.98, 1.3, 0.7, 0, 0, 0, 30.4615246539631, 29.5168052445698, 
28.5386567531972, 27.5240625110885, 26.4695358481376, 25.3710104120508, 
24.2236949210448, 23.0218770415655, 21.7586525900181, 20.4255418047447, 
19.0119287856144, 17.504212384863, 15.8844623158238, 14.128173601714, 
12.2002469898736, 10.0471121373362, 7.57923510617568, 4.62429214263011, 
0.75431137340667, 0, 22.7106358073296, 21.6936284816533, 20.6741988279281, 
19.6520698103227, 18.6269100117322, 17.5983186354345, 16.56580505824, 
15.5287604082975, 14.4864171707797, 13.4377902927422, 12.3815887160111, 
11.3160777183433, 10.2388554102512, 9.14647035671571, 8.0337225650011, 
6.89226940611587, 5.70748958027703, 4.45003631147136, 3.04480970842971, 
1.11763700580719), Tvec = c(1.48387096774194, 1.4375, 1.39393939393939, 
1.35294117647059, 1.31428571428571, 1.27777777777778, 1.24324324324324, 
1.21052631578947, 1.17948717948718, 1.15, 1.1219512195122, 1.0952380952381, 
1.06976744186047, 1.04545454545455, 1.02222222222222, 3, 2.9, 
2.8, 2.7, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2, 1.9, 1.8, 1.7, 1.6, 
1.5, 1.4, 1.3, 1.2, 1.1, 3, 2.9, 2.8, 2.7, 2.6, 2.5, 2.4, 2.3, 
2.2, 2.1, 2, 1.9, 1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1), lines = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), points = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), CIlow90 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 19.9153252154534, 19.2409639000826, 18.5479348354799, 
17.8346854503175, 17.0994298362258, 16.3400951314142, 15.5542506180987, 
14.7390121055676, 13.8909100510847, 13.0057028605436, 12.0781043721521, 
11.1013713463383, 10.06665098751, 8.96189138084694, 7.7698926228238, 
6.464491892272, 4.84825533737019, 2.93589455149082, 0, 0, 16.0577098270068, 
15.2518748648627, 14.4510159298979, 13.7131160072482, 12.978704270505, 
12.218035777573, 11.381568380686, 10.5506209566196, 9.73893724905702, 
8.97090549187465, 8.20806022358252, 7.45003855272051, 6.69621847442936, 
5.94554942726675, 5.19623609148578, 4.44509679122497, 3.68609975994477, 
2.87341020597837, 2.00573453139687, 0), CIhigh90 = c(NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39.5496128179917, 
38.4658703630674, 37.2873172707213, 36.0587182906439, 34.7750340732398, 
33.4303655719391, 32.0177357215266, 30.5287941081476, 28.9534084668307, 
27.2790848307336, 25.4901190620958, 23.5663095538808, 21.4809165627524, 
19.2623067546032, 16.8905403124527, 14.2002290733817, 11.0601936227834, 
7.24788989408184, 2.40893162529783, 0, 32.617505639867, 31.6590035077607, 
30.6188417063026, 29.4234719001571, 28.199236779321, 26.9435080239238, 
25.6532048923666, 24.3246726964608, 22.9550588212613, 21.5477285350574, 
20.0832734626163, 18.5527014194524, 16.9441932581813, 15.2416351113752, 
13.4219895205259, 11.4501435135235, 9.41874997589279, 7.20320099713344, 
4.5425628599556, 2.46061882893576), CIlow95 = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 18.752695279292, 
18.0788353923051, 17.3876881999595, 16.677845612184, 15.9476894910084, 
15.1953434825654, 14.4186093168955, 13.6538256394755, 12.8624294747331, 
12.0374199414744, 11.1740395930956, 10.2662214266024, 9.30600243357516, 
8.28254420995867, 7.18038102315942, 5.97599027949699, 4.61181065121508, 
2.6976558388846, 0, 0, 18.752695279292, 18.0788353923051, 17.3876881999595, 
16.677845612184, 15.9476894910084, 15.1953434825654, 14.4186093168955, 
13.6538256394755, 12.8624294747331, 12.0374199414744, 11.1740395930956, 
10.2662214266024, 9.30600243357516, 8.28254420995867, 7.18038102315942, 
5.97599027949699, 4.61181065121508, 2.6976558388846, 0, 0), CIhigh95 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39.8875457837325, 
38.8064729140871, 37.7301836947564, 36.6052411433444, 35.4266144726828, 
34.1884023758168, 32.8836101800222, 31.5038480349679, 30.0389129495536, 
28.4761948177072, 26.7998062780988, 24.9892610227647, 23.0173762089904, 
20.8467578067284, 18.4234910352706, 15.6647366191272, 12.43108133462, 
8.27439427361177, 2.61839588710604, 0, 39.8875457837325, 38.8064729140871, 
37.7301836947564, 36.6052411433444, 35.4266144726828, 34.1884023758168, 
32.8836101800222, 31.5038480349679, 30.0389129495536, 28.4761948177072, 
26.7998062780988, 24.9892610227647, 23.0173762089904, 20.8467578067284, 
18.4234910352706, 15.6647366191272, 12.43108133462, 8.27439427361177, 
2.61839588710604, 0), L1 = c("dfpp", "dfpp", "dfpp", "dfpp", 
"dfpp", "dfpp", "dfpp", "dfpp", "dfpp", "dfpp", "dfpp", "dfpp", 
"dfpp", "dfpp", "dfpp", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", 
"dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", 
"dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", 
"dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevpwm", "dfgevmle", "dfgevmle", 
"dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", 
"dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", 
"dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle", "dfgevmle"
)), .Names = c("Floods", "Tvec", "lines", "points", "CIlow90", 
"CIhigh90", "CIlow95", "CIhigh95", "L1"), row.names = c("31", 
"32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", 
"43", "44", "45", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "311", 
"321", "331", "341", "351", "361", "371", "381", "391", "401"
), class = "data.frame")

我想将两个图例结合起来,这样我只有分布图例,而背景色是色带图例。

1 个答案:

答案 0 :(得分:1)

要将两个手册(此处为颜色和填充)组合为一个图例,必须完全相同地指定两个图例,因此

select a.* from entry_data_fxj_cl a left join exit_data b 
    on trim(a.ecardid) = trim(b.ecardid) and abs(a.entrytime-b.entrytime)>60000
    where trim(b.ecardid) IS NULL 

我不得不用ylim注释这行,因为它使用了您未提供的数据,因为它应该可以工作。