带有两个手动色标的ggplot

时间:2018-10-19 15:37:04

标签: r ggplot2

我正在尝试重用一些以前用来制作两个独立图的旧代码,现在想将其一起拼成一个。
但是我没什么问题

color_var <- vector(mode = "double",length = length(OP_2016$risk))
color_var[color_var== '0']<- NA 
color_var[OP_2016$risk>=1 & OP_2016$risk<12] <- "yellow"
color_var[OP_2016$risk>=12] <- "red"

ggplot(OP_2016)+ 
      geom_col(aes(x = short_date, y = risk, color = color_var , group = 1), size= 0.9) +
      scale_y_continuous(limits = c(0, 100),name = "Accumulated EBHours")+
      scale_color_identity("Risk Level", breaks= levels(as.factor(color_var))[c(1,2)], 
                           labels = c("High >12 EBH","Medium 0-12EBH"),
                           guide = "legend"
                           )+ 
      geom_line(aes(x  = short_date, y= 12), linetype= "dotted", size = 0.8, colour = "red")+
      # scale_color_manual("Varieties", values =c( "British Queen"= "orchid1"))+
      geom_line(data = dis_fun_df, aes(x= date, y = rating, colour = "green"))

问题:

  • 图例中的条和框未填充
  • 我无法为geom_line添加手动颜色并将其添加到从其他绘图中添加的图例。
抱歉,用于重现情节的数据集有点大。

dis_fun_df <-  structure(list(date = structure(c(15534, 15540, 15548, 15555, 
    15562, 15573, 15580), class = "Date"), rating = c(10.2, 30, 61.6666666666667, 
    81.6666666666667, 95.8333333333333, 99.1666666666667, 100)), row.names = c(NA, 
    -7L), class = c("tbl_df", "tbl", "data.frame"))

OP_2016 <- structure(list(date = structure(c(1342224000, 1342227600, 1342231200, 
1342234800, 1342238400, 1342242000, 1342245600, 1342249200, 1342252800, 
1342256400, 1342260000, 1342263600, 1342267200, 1342270800, 1342274400, 
1342278000, 1342281600, 1342285200, 1342288800, 1342292400, 1342296000, 
1342299600, 1342303200, 1342306800, 1342310400, 1342314000, 1342317600, 
1342321200, 1342324800, 1342328400, 1342332000, 1342335600, 1342339200, 
1342342800, 1342346400, 1342350000, 1342353600, 1342357200, 1342360800, 
1342364400, 1342368000, 1342371600, 1342375200, 1342378800, 1342382400, 
1342386000, 1342389600, 1342393200, 1342396800, 1342400400, 1342404000, 
1342407600, 1342411200, 1342414800, 1342418400, 1342422000, 1342425600, 
1342429200, 1342432800, 1342436400, 1342440000, 1342443600, 1342447200, 
1342450800, 1342454400, 1342458000, 1342461600, 1342465200, 1342468800, 
1342472400, 1342476000, 1342479600, 1342483200, 1342486800, 1342490400, 
1342494000, 1342497600, 1342501200, 1342504800, 1342508400, 1342512000, 
1342515600, 1342519200, 1342522800, 1342526400, 1342530000, 1342533600, 
1342537200, 1342540800, 1342544400, 1342548000, 1342551600, 1342555200, 
1342558800, 1342562400, 1342566000, 1342569600, 1342573200, 1342576800, 
1342580400, 1342584000, 1342587600, 1342591200, 1342594800, 1342598400, 
1342602000, 1342605600, 1342609200, 1342612800, 1342616400, 1342620000, 
1342623600, 1342627200, 1342630800, 1342634400, 1342638000, 1342641600, 
1342645200, 1342648800, 1342652400, 1342656000, 1342659600, 1342663200, 
1342666800, 1342670400, 1342674000, 1342677600, 1342681200, 1342684800, 
1342688400, 1342692000, 1342695600, 1342699200, 1342702800, 1342706400, 
1342710000, 1342713600, 1342717200, 1342720800, 1342724400, 1342728000, 
1342731600, 1342735200, 1342738800, 1342742400, 1342746000, 1342749600, 
1342753200, 1342756800, 1342760400, 1342764000, 1342767600, 1342771200, 
1342774800, 1342778400, 1342782000, 1342785600, 1342789200, 1342792800, 
1342796400, 1342800000, 1342803600, 1342807200, 1342810800, 1342814400, 
1342818000, 1342821600, 1342825200, 1342828800, 1342832400, 1342836000, 
1342839600, 1342843200, 1342846800, 1342850400, 1342854000, 1342857600, 
1342861200, 1342864800, 1342868400, 1342872000, 1342875600, 1342879200, 
1342882800, 1342886400, 1342890000, 1342893600, 1342897200, 1342900800, 
1342904400, 1342908000, 1342911600, 1342915200, 1342918800, 1342922400, 
1342926000, 1342929600, 1342933200, 1342936800, 1342940400, 1342944000, 
1342947600, 1342951200, 1342954800, 1342958400, 1342962000, 1342965600, 
1342969200, 1342972800, 1342976400, 1342980000, 1342983600, 1342987200, 
1342990800, 1342994400, 1342998000, 1343001600, 1343005200, 1343008800, 
1343012400, 1343016000, 1343019600, 1343023200, 1343026800, 1343030400, 
1343034000, 1343037600, 1343041200, 1343044800, 1343048400, 1343052000, 
1343055600, 1343059200, 1343062800, 1343066400, 1343070000, 1343073600, 
1343077200, 1343080800, 1343084400, 1343088000, 1343091600, 1343095200, 
1343098800, 1343102400, 1343106000, 1343109600, 1343113200, 1343116800, 
1343120400, 1343124000, 1343127600, 1343131200, 1343134800, 1343138400, 
1343142000, 1343145600, 1343149200, 1343152800, 1343156400, 1343160000, 
1343163600, 1343167200, 1343170800, 1343174400, 1343178000, 1343181600, 
1343185200, 1343188800, 1343192400, 1343196000, 1343199600, 1343203200, 
1343206800, 1343210400, 1343214000, 1343217600, 1343221200, 1343224800, 
1343228400, 1343232000, 1343235600, 1343239200, 1343242800, 1343246400, 
1343250000, 1343253600, 1343257200, 1343260800, 1343264400, 1343268000, 
1343271600, 1343275200, 1343278800, 1343282400, 1343286000, 1343289600, 
1343293200, 1343296800, 1343300400, 1343304000, 1343307600, 1343311200, 
1343314800, 1343318400, 1343322000, 1343325600, 1343329200, 1343332800, 
1343336400, 1343340000, 1343343600, 1343347200, 1343350800, 1343354400, 
1343358000, 1343361600, 1343365200, 1343368800, 1343372400, 1343376000, 
1343379600, 1343383200, 1343386800, 1343390400, 1343394000, 1343397600, 
1343401200, 1343404800, 1343408400, 1343412000, 1343415600, 1343419200, 
1343422800, 1343426400, 1343430000, 1343433600, 1343437200, 1343440800, 
1343444400, 1343448000, 1343451600, 1343455200, 1343458800, 1343462400, 
1343466000, 1343469600, 1343473200, 1343476800, 1343480400, 1343484000, 
1343487600, 1343491200, 1343494800, 1343498400, 1343502000, 1343505600, 
1343509200, 1343512800, 1343516400, 1343520000, 1343523600, 1343527200, 
1343530800, 1343534400, 1343538000, 1343541600, 1343545200, 1343548800, 
1343552400, 1343556000, 1343559600, 1343563200, 1343566800, 1343570400, 
1343574000, 1343577600, 1343581200, 1343584800, 1343588400, 1343592000, 
1343595600, 1343599200, 1343602800, 1343606400, 1343610000, 1343613600, 
1343617200, 1343620800, 1343624400, 1343628000, 1343631600, 1343635200, 
1343638800, 1343642400, 1343646000, 1343649600, 1343653200, 1343656800, 
1343660400, 1343664000, 1343667600, 1343671200, 1343674800, 1343678400, 
1343682000, 1343685600, 1343689200, 1343692800, 1343696400, 1343700000, 
1343703600, 1343707200, 1343710800, 1343714400, 1343718000, 1343721600, 
1343725200, 1343728800, 1343732400, 1343736000, 1343739600, 1343743200, 
1343746800, 1343750400, 1343754000, 1343757600, 1343761200, 1343764800, 
1343768400, 1343772000, 1343775600, 1343779200, 1343782800, 1343786400, 
1343790000, 1343793600, 1343797200, 1343800800, 1343804400, 1343808000, 
1343811600, 1343815200, 1343818800, 1343822400, 1343826000, 1343829600, 
1343833200, 1343836800, 1343840400, 1343844000, 1343847600, 1343851200, 
1343854800, 1343858400, 1343862000, 1343865600, 1343869200, 1343872800, 
1343876400, 1343880000, 1343883600, 1343887200, 1343890800, 1343894400, 
1343898000, 1343901600, 1343905200, 1343908800, 1343912400, 1343916000, 
1343919600, 1343923200, 1343926800, 1343930400, 1343934000, 1343937600, 
1343941200, 1343944800, 1343948400, 1343952000, 1343955600, 1343959200, 
1343962800, 1343966400, 1343970000, 1343973600, 1343977200, 1343980800, 
1343984400, 1343988000, 1343991600, 1343995200, 1343998800, 1344002400, 
1344006000, 1344009600, 1344013200, 1344016800, 1344020400, 1344024000, 
1344027600, 1344031200, 1344034800, 1344038400, 1344042000, 1344045600, 
1344049200, 1344052800, 1344056400, 1344060000, 1344063600, 1344067200, 
1344070800, 1344074400, 1344078000, 1344081600, 1344085200, 1344088800, 
1344092400, 1344096000, 1344099600, 1344103200, 1344106800, 1344110400, 
1344114000, 1344117600, 1344121200, 1344124800, 1344128400, 1344132000, 
1344135600, 1344139200, 1344142800, 1344146400, 1344150000, 1344153600, 
1344157200, 1344160800, 1344164400, 1344168000, 1344171600, 1344175200, 
1344178800, 1344182400, 1344186000, 1344189600, 1344193200, 1344196800, 
1344200400, 1344204000, 1344207600, 1344211200, 1344214800, 1344218400, 
1344222000, 1344225600, 1344229200, 1344232800, 1344236400, 1344240000, 
1344243600, 1344247200, 1344250800, 1344254400, 1344258000, 1344261600, 
1344265200, 1344268800, 1344272400, 1344276000, 1344279600, 1344283200, 
1344286800, 1344290400, 1344294000, 1344297600, 1344301200, 1344304800, 
1344308400, 1344312000, 1344315600, 1344319200, 1344322800, 1344326400, 
1344330000, 1344333600, 1344337200, 1344340800, 1344344400, 1344348000, 
1344351600, 1344355200, 1344358800, 1344362400, 1344366000, 1344369600, 
1344373200, 1344376800, 1344380400, 1344384000, 1344387600, 1344391200, 
1344394800, 1344398400, 1344402000, 1344405600, 1344409200, 1344412800, 
1344416400, 1344420000, 1344423600, 1344427200, 1344430800, 1344434400, 
1344438000, 1344441600, 1344445200, 1344448800, 1344452400, 1344456000, 
1344459600, 1344463200, 1344466800, 1344470400, 1344474000, 1344477600, 
1344481200, 1344484800, 1344488400, 1344492000, 1344495600, 1344499200, 
1344502800, 1344506400, 1344510000, 1344513600, 1344517200, 1344520800, 
1344524400, 1344528000, 1344531600, 1344535200, 1344538800, 1344542400, 
1344546000, 1344549600, 1344553200, 1344556800, 1344560400, 1344564000, 
1344567600, 1344571200, 1344574800, 1344578400, 1344582000, 1344585600, 
1344589200, 1344592800, 1344596400, 1344600000, 1344603600, 1344607200, 
1344610800, 1344614400, 1344618000, 1344621600, 1344625200, 1344628800, 
1344632400, 1344636000, 1344639600, 1344643200, 1344646800, 1344650400, 
1344654000, 1344657600, 1344661200, 1344664800, 1344668400, 1344672000, 
1344675600, 1344679200, 1344682800, 1344686400, 1344690000, 1344693600, 
1344697200, 1344700800, 1344704400, 1344708000, 1344711600, 1344715200, 
1344718800, 1344722400, 1344726000, 1344729600, 1344733200, 1344736800, 
1344740400, 1344744000, 1344747600, 1344751200, 1344754800, 1344758400, 
1344762000, 1344765600, 1344769200, 1344772800, 1344776400, 1344780000, 
1344783600, 1344787200, 1344790800, 1344794400, 1344798000, 1344801600, 
1344805200, 1344808800, 1344812400, 1344816000, 1344819600, 1344823200, 
1344826800, 1344830400, 1344834000, 1344837600, 1344841200, 1344844800, 
1344848400, 1344852000, 1344855600, 1344859200, 1344862800, 1344866400, 
1344870000, 1344873600, 1344877200, 1344880800, 1344884400, 1344888000, 
1344891600, 1344895200, 1344898800, 1344902400, 1344906000, 1344909600, 
1344913200, 1344916800, 1344920400, 1344924000, 1344927600, 1344931200, 
1344934800, 1344938400, 1344942000, 1344945600, 1344949200, 1344952800, 
1344956400, 1344960000, 1344963600, 1344967200, 1344970800, 1344974400, 
1344978000, 1344981600, 1344985200, 1344988800, 1344992400, 1344996000, 
1344999600, 1345003200, 1345006800, 1345010400, 1345014000, 1345017600, 
1345021200, 1345024800, 1345028400, 1345032000, 1345035600, 1345039200, 
1345042800, 1345046400, 1345050000, 1345053600, 1345057200, 1345060800, 
1345064400, 1345068000, 1345071600, 1345075200, 1345078800, 1345082400, 
1345086000, 1345089600, 1345093200, 1345096800, 1345100400, 1345104000, 
1345107600, 1345111200, 1345114800, 1345118400, 1345122000, 1345125600, 
1345129200, 1345132800, 1345136400, 1345140000, 1345143600, 1345147200, 
1345150800, 1345154400, 1345158000, 1345161600, 1345165200, 1345168800, 
1345172400, 1345176000, 1345179600, 1345183200, 1345186800, 1345190400, 
1345194000, 1345197600, 1345201200, 1345204800, 1345208400, 1345212000, 
1345215600, 1345219200, 1345222800, 1345226400, 1345230000, 1345233600, 
1345237200, 1345240800, 1345244400, 1345248000, 1345251600, 1345255200, 
1345258800, 1345262400, 1345266000, 1345269600, 1345273200, 1345276800, 
1345280400, 1345284000, 1345287600, 1345291200, 1345294800, 1345298400, 
1345302000, 1345305600, 1345309200, 1345312800, 1345316400, 1345320000, 
1345323600, 1345327200, 1345330800, 1345334400, 1345338000, 1345341600, 
1345345200, 1345348800, 1345352400, 1345356000, 1345359600, 1345363200, 
1345366800, 1345370400, 1345374000, 1345377600, 1345381200, 1345384800, 
1345388400, 1345392000, 1345395600, 1345399200, 1345402800, 1345406400, 
1345410000, 1345413600, 1345417200, 1345420800, 1345424400, 1345428000, 
1345431600, 1345435200, 1345438800, 1345442400, 1345446000, 1345449600, 
1345453200, 1345456800, 1345460400, 1345464000, 1345467600, 1345471200, 
1345474800, 1345478400, 1345482000, 1345485600, 1345489200, 1345492800, 
1345496400, 1345500000, 1345503600, 1345507200, 1345510800, 1345514400, 
1345518000, 1345521600, 1345525200, 1345528800, 1345532400, 1345536000, 
1345539600, 1345543200, 1345546800, 1345550400, 1345554000, 1345557600, 
1345561200, 1345564800, 1345568400, 1345572000, 1345575600, 1345579200, 
1345582800, 1345586400, 1345590000, 1345593600, 1345597200, 1345600800, 
1345604400, 1345608000, 1345611600, 1345615200, 1345618800, 1345622400, 
1345626000, 1345629600, 1345633200, 1345636800, 1345640400, 1345644000, 
1345647600, 1345651200, 1345654800, 1345658400, 1345662000, 1345665600, 
1345669200, 1345672800, 1345676400, 1345680000, 1345683600, 1345687200, 
1345690800, 1345694400, 1345698000, 1345701600, 1345705200, 1345708800, 
1345712400, 1345716000, 1345719600, 1345723200, 1345726800, 1345730400, 
1345734000, 1345737600, 1345741200, 1345744800, 1345748400, 1345752000, 
1345755600, 1345759200, 1345762800, 1345766400, 1345770000, 1345773600, 
1345777200, 1345780800, 1345784400, 1345788000, 1345791600, 1345795200, 
1345798800, 1345802400, 1345806000, 1345809600, 1345813200, 1345816800, 
1345820400, 1345824000, 1345827600, 1345831200, 1345834800, 1345838400, 
1345842000, 1345845600, 1345849200, 1345852800, 1345856400, 1345860000, 
1345863600, 1345867200, 1345870800, 1345874400, 1345878000, 1345881600, 
1345885200, 1345888800, 1345892400, 1345896000, 1345899600, 1345903200, 
1345906800, 1345910400, 1345914000, 1345917600, 1345921200, 1345924800, 
1345928400, 1345932000, 1345935600, 1345939200, 1345942800, 1345946400, 
1345950000, 1345953600, 1345957200, 1345960800, 1345964400, 1345968000, 
1345971600, 1345975200, 1345978800, 1345982400, 1345986000, 1345989600, 
1345993200, 1345996800, 1346000400, 1346004000, 1346007600, 1346011200, 
1346014800, 1346018400, 1346022000, 1346025600, 1346029200, 1346032800, 
1346036400, 1346040000, 1346043600, 1346047200, 1346050800, 1346054400, 
1346058000, 1346061600, 1346065200, 1346068800, 1346072400, 1346076000, 
1346079600, 1346083200, 1346086800, 1346090400, 1346094000, 1346097600, 
1346101200, 1346104800, 1346108400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), risk = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 2, 3, 4, 5,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 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0)), row.names = c(NA, -1080L), class = c("tbl_df", 
"tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

我认为使用填充而不是颜色可以解决问题

OP_2016$date <- as.Date(OP_2016$date)

color_var <- vector(mode = "double",length = length(OP_2016$risk))
color_var[color_var== '0']<- NA 
color_var[OP_2016$risk>=1 & OP_2016$risk<12] <- "yellow"
color_var[OP_2016$risk>=12] <- "red"

ggplot(OP_2016)+ 
    geom_col(aes(x = date, y = risk,  group = 1,fill=color_var), size= 0.9) +
    scale_y_continuous(limits = c(0, 100),name = "Accumulated EBHours")+
    scale_fill_identity("Risk Level", breaks= levels(as.factor(color_var))[c(1,2)], 
                        labels = c("High >12 EBH","Medium 0-12EBH"),
                        guide = "legend"
    )+ 
    geom_line(aes(x  = date, y= 12), linetype= "dotted", size = 0.8, colour = "red")+
    geom_line(data = dis_fun_df, aes(x= date, y = rating),colour = "green")

据我所知,ggplot不支持同一类型的多个音阶,但其他人会比我更了解。