根据第三个变量为ggplot2图添加背景

时间:2017-08-06 20:52:36

标签: r ggplot2

我想基于第三个变量(下面数据中的变量p)为下图添加背景。但是,我无法让它发挥作用。之前有一些相关帖子,如for instance this one。但是,它似乎不适用于我的情况。

enter image description here

数据

out121<-structure(list(MEp_R = c(-0.00798875313192999, -0.0148676123843391, 
                                 -0.00682061802389229, -0.00342320747900698, -0.0151338635205871, 
                                 -0.0107317027491587, -0.0251293011821259, -0.00442888716467913, 
                                 -0.00336413112985843, -0.00684252019402579, -0.0179441683421641, 
                                 -0.0183345869013467, -0.00648428720725909, -0.0214876248035767, 
                                 -0.016436676184706, -0.0374053532385283, -0.0149373698200334, 
                                 -0.00812201774657575, -0.00845365671158477, -0.00770113356603315, 
                                 -0.0235236091649431, -0.00319728180470263, -0.00375490467048723, 
                                 -0.0105497059284769, -0.0113949568001174, -0.00796452607813328, 
                                 -0.018162596356658, -0.0305161849884928, -0.0182695024428088, 
                                 -0.00733874764247203, -0.0258767466756409, -0.00800032458837883, 
                                 -0.0254092616701198, -0.00796500399596014, -0.018098828213091, 
                                 -0.00403853247796117, -0.00537848845812282, -0.0150529001843169, 
                                 -0.0184434922700636, -0.028401757025326, -0.00394247092784099, 
                                 -0.00389984372897549, -0.0212293138913338, -0.00323995037136412, 
                                 -0.00435171103575741, -0.0142661346880167, -0.00763574799777637, 
                                 -0.0243480163192293, -0.0334550531174489, -0.00398665257789114, 
                                 -0.0269010788141356, -0.0339462390051263, -0.00731023368759553, 
                                 -0.00273217917938973, -0.00721679095419613, -0.0284960447574926, 
                                 -0.0256689850381392, -0.00418816161208601, -0.0207469376148546, 
                                 -0.0159256736739084, -0.00313049343988148, -0.0280083118773398, 
                                 -0.00305634712789695, -0.00222991072333599, -0.00728714579423012, 
                                 -0.00807506060034409, -0.0152635924760586, -0.0332733629111364, 
                                 -0.00760293035610356, -0.0139815979579726, -0.00677123700835164, 
                                 -0.0215960340688805, -0.0338156227799011, -0.0253065053249637, 
                                 -0.018647649832361, -0.0354963987663701, -0.022253017915301, 
                                 -0.00671809517372551, -0.00732452244298519, -0.0319724784878375, 
                                 -0.0342471218743405, -0.0350620385653106, -0.0230236881980878, 
                                 -0.0346963784053919, -0.00282689342327851, -0.00325484604261175, 
                                 -0.033849668042083, -0.00413139898221564, -0.0228993329834933, 
                                 -0.00559908152238757, -0.0183537460976775, -0.00472047771208315, 
                                 -0.0251909804124543, -0.0311011436015412, -0.0229936133519116, 
                                 -0.00308542036103775, -0.0228912921993044, -0.012939181841082, 
                                 -0.0230877632656367, -0.032591064432755, -0.00735469284998116, 
                                 -0.0303916947589657, -0.00314093237962542, -0.0231490730231191, 
                                 -0.0054652608506885, -0.025241520518079, -0.00445136785085343, 
                                 -0.0128593850454578, -0.00673762742746708, -0.0226916858622337, 
                                 -0.0161806428228128, -0.0209040965403646, -0.020750711338677, 
                                 -0.0181057154876848, -0.0178080592822426, -0.0176328580881027, 
                                 -0.0176110802336996, -0.0176155620180982, -0.0178561500821362, 
                                 -0.0154770868242239, -0.0155229482583411, -0.0180674673481902, 
                                 -0.0178800497751207, -0.0154828650185023, -0.0154750397213182, 
                                 -0.0152368261892578, -0.0154977482766045, -0.0182346532616592, 
                                 -0.0183274537639035, -0.0168011783244315, -0.0168016019401702, 
                                 -0.0149946562681369, -0.0158317352920475, -0.0167520543181098, 
                                 -0.0166157299693747, -0.0158279779461525, -0.0149977762443961, 
                                 -0.0180750003460435, -0.0151529841212906, -0.017923432133514, 
                                 -0.017928916003054, -0.0179127560726451, -0.0180570721490242, 
                                 -0.0177899027947602, -0.0158531006505291, -0.0174593473238637, 
                                 -0.0151291673194569, -0.0179473924690324, -0.017693119507179, 
                                 -0.0177884007129345, -0.0148359633238028, -0.0178372767093443, 
                                 -0.0179573607500675, -0.0178478763689906, -0.0159047471769866, 
                                 -0.0179332447429962, -0.0148569280408044, -0.0179181675050371, 
                                 -0.0166326656412793, -0.0148692975727, -0.0159248001099628, -0.0181012191592349, 
                                 -0.0178451421528243, -0.0165514761611982, -0.0157213399905123, 
                                 -0.0157870112143077), MEp_Irr = c(0.0267063409682918, 0.0348657385506534, 
                                                                   0.0342691355007968, -0.115640975842031, 0.0387492569408993, 0.0381593243875236, 
                                                                   0.0427698849005908, -0.0173766138908777, -0.137295803791003, 
                                                                   0.0255837365606775, 0.033333234010886, 0.04219733855297, 0.0283972511296412, 
                                                                   0.042063245938688, 0.0381434891186468, 0.116843385842082, 0.0303337697359422, 
                                                                   0.0270649725588233, 0.0408800022550086, 0.0212509208656095, 0.0411728527295687, 
                                                                   -0.124549799025409, -0.0144478716604061, 0.0423901861335305, 
                                                                   0.0415985024073151, 0.037704775866327, 0.0422604508495823, 0.0763588031748094, 
                                                                   0.0426357961926886, 0.0329608493967286, 0.0496266735787044, 0.040016714764635, 
                                                                   0.0519859953665068, 0.0394245716448228, 0.0342680692030386, -0.0267193515702238, 
                                                                   -0.0177971867890813, 0.0381256479675564, 0.0355367325792796, 
                                                                   0.0727607935148329, -0.0326388375297887, -0.0178079477006394, 
                                                                   0.0410572010103166, -0.117374678254156, -0.020320609244612, 0.0370392597214022, 
                                                                   0.0353728091272744, 0.0449625606410676, 0.0753850352272533, -0.0343858665221561, 
                                                                   0.0698051617341533, 0.077951540991422, 0.0237442438741627, -0.058297571549435, 
                                                                   0.0243284890185037, 0.0772458962449727, 0.0543866044441634, -0.00428096536358291, 
                                                                   0.04038564508393, 0.0354424565177792, -0.131352692076439, 0.0720546321152786, 
                                                                   -0.0665837597449708, -0.127968519873726, 0.0330986089266455, 
                                                                   0.0259981472748394, 0.0335310580145865, 0.0796945930317802, 0.020663774953603, 
                                                                   0.0293980728446607, 0.0362259277862459, 0.0432046089419517, 0.0685995447124759, 
                                                                   0.0469963521357076, 0.0354789715561056, 0.0709742762976496, 0.0452791300187091, 
                                                                   0.0331219798716994, 0.0295434908925642, 0.08744851179291, 0.0799768054034634, 
                                                                   0.0815503850718988, 0.0440574836496075, 0.0839865621030131, -0.0688754800689186, 
                                                                   -0.116779517177347, 0.0694952520459083, -0.0264949177920162, 
                                                                   0.0413932242242241, 0.0172278601886947, 0.0420753996022112, -0.144255178922228, 
                                                                   0.0470894777847475, 0.076135949143235, 0.041744283972074, -0.0630340966211796, 
                                                                   0.0413829074872194, 0.0396332210621789, 0.041111622411323, 0.102474470476636, 
                                                                   0.0325757173791441, 0.0760653479136575, -0.123211363037203, 0.0410342604744772, 
                                                                   -0.0174831909563214, 0.0530941558728644, -0.0418843636393472, 
                                                                   0.038435679009942, 0.0336208563560449, 0.0399056440442261, 0.0265540871197283, 
                                                                   0.0444367528340963, 0.0435895606359242, 0.0215615450224718, 0.0209324963084151, 
                                                                   0.0202690410290722, 0.0194304147117461, 0.0197015376925802, 0.0232944851025464, 
                                                                   0.0194971575601002, 0.0195128222684842, 0.0229793934050297, 0.022601136427616, 
                                                                   0.020063571212835, 0.0199659939198503, 0.0189019086482273, 0.020421297722678, 
                                                                   0.0238829270946739, 0.0242153520276344, 0.0261331103214652, 0.0259380402516094, 
                                                                   0.0195777353030817, 0.0234436006049973, 0.0253584791696736, 0.025820695095831, 
                                                                   0.0242819014695358, 0.0195352848772439, 0.0287263051849994, 0.0195826041035442, 
                                                                   0.025491942513768, 0.0244647468662558, 0.0257233178691396, 0.0262291526865683, 
                                                                   0.0247672209152699, 0.0245726192470798, 0.0273106213867739, 0.0199937372665979, 
                                                                   0.027112767769544, 0.027267180444377, 0.0261432756569257, 0.0198848926943087, 
                                                                   0.0256279615107693, 0.0279085914888886, 0.0285149093762463, 0.0240689981341775, 
                                                                   0.0266901583531034, 0.0200700646179756, 0.0262578329932839, 0.0263488901038499, 
                                                                   0.0201551487864926, 0.0244667611324202, 0.0262710081046724, 0.0264923641418946, 
                                                                   0.0252091848629891, 0.0230331062657154, 0.0230721882488657), 
                       p = 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, 1.33226762955019e-15, 
                             1.99840144432528e-15, 1.31006316905768e-14, 2.42694753183059e-13, 
                             3.68407526707415e-10, 1.61860724823271e-08, 1.27863204824052e-06, 
                             2.38962085785666e-06, 4.89028683459125e-06, 1.29586352866085e-05, 
                             1.3194564602248e-05, 5.02157467259234e-05, 6.66173260168712e-05, 
                             0.000452773851216115, 0.00058440281230121, 0.000733510241929469, 
                             0.000880660946449119, 0.0136063339510712, 0.0151237168548795, 
                             0.0353698786443037, 0.0615641433741911, 0.0627726749529298, 
                             0.0763356770320813, 0.0824154857507584, 0.0929893345348154, 
                             0.0947385432373617, 0.0958710673877281, 0.104039435798143, 
                             0.117891950799349, 0.137872040413549, 0.173995516565528, 
                             0.216611706295959, 0.277748137930015, 0.29066025684483, 0.294964122909925, 
                             0.360934324888994, 0.390659280716865, 0.399257628135196, 
                             0.415075121018395, 0.415906223140706, 0.429535524186567, 
                             0.4346739056448, 0.461459259127042, 0.492938667179821, 0.552802494760443, 
                             0.606427873500538, 0.608777827159917, 0.621809390011274, 
                             0.65212833158726, 0.720281808796569, 0.725498755645982, 0.732029138010702, 
                             0.873071745150822, 0.902596817430848, 0.965286951991386, 
                             0.979065101832578)), datalabel = "Written by R.              ", time.stamp = " 6 Aug 2017 22:23", .Names = c("MEp_R", 
                                                                                                                                          "MEp_Irr", "p"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(255L, 
                                                                                                                                                                                                             255L, 255L), val.labels = c("", "", ""), var.labels = c("MEp_R", 
                                                                                                                                                                                                                                                                     "MEp_Irr", "p"), row.names = c("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", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
                                                                                                                                                                                                                                                                                                    "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", 
                                                                                                                                                                                                                                                                                                    "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", 
                                                                                                                                                                                                                                                                                                    "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", 
                                                                                                                                                                                                                                                                                                    "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", 
                                                                                                                                                                                                                                                                                                    "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", 
                                                                                                                                                                                                                                                                                                    "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", 
                                                                                                                                                                                                                                                                                                    "105", "106", "107", "108", "109", "110", "111", "112", "113", 
                                                                                                                                                                                                                                                                                                    "114", "115", "116", "117", "118", "119", "120", "121", "122", 
                                                                                                                                                                                                                                                                                                    "123", "124", "125", "126", "127", "128", "129", "130", "131", 
                                                                                                                                                                                                                                                                                                    "132", "133", "134", "135", "136", "137", "138", "139", "140", 
                                                                                                                                                                                                                                                                                                    "141", "142", "143", "144", "145", "146", "147", "148", "149", 
                                                                                                                                                                                                                                                                                                    "150", "151", "152", "153", "154", "155", "156", "157", "158", 
                                                                                                                                                                                                                                                                                                    "159", "160", "161", "162", "163", "164", "165", "166"), version = 12L, class = "data.frame")

THE PLOT

out121$perc1<-c(1:166)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEp_R,colour="var1"),size=1.3)+
  geom_line(aes(y=MEp_Irr,colour="var2"),size=1.3)+
  scale_colour_manual(values=c("blue3","green"), name="MEt")+
  scale_x_discrete(name="Threshold (in percentage)",breaks=c(0, 250, 500,750,1000),
                   labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="MEp",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample ") + 
  theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
  guides(col=guide_legend(ncol=2,title.position="top"))+
  theme(panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

问题

我现在想根据变量&#34; p&#34;为此情节添加背景。 变量只有一些大于0.05的值。因此,只有这些点背景应该是彩色的。 这意味着,低于0.05的点应该具有白色背景,其他点应该具有红色(例如)背景。

我根据以下代码做了一些不幸的努力,但我没有得到缩放(白/蓝右,甚至不连续),我不能在背景中获得颜色。有人可以帮帮我吗?非常感谢你!

geom_rect(aes(xmin = 0, xmax = 1000, ymin = -Inf, ymax = Inf,fill = p))+
  scale_color_gradient2(midpoint=0.05, low="white", mid="light blue",high="blue", space ="Lab" )

我遇到的一个错误是: &#34;缩放&#39;颜色&#39;已经 当下。添加另一个比例 &#39; color&#39;,它将取代 现有规模。&#34;但即使这个问题得到解决,也无法解决我背景图上只有一种颜色的事实。

1 个答案:

答案 0 :(得分:2)

首先

out121$p_above <- out121$p > 0.05

然后

l66 + geom_rect(aes(xmin = perc1 - 0.5, xmax = perc1 + 0.5, 
                    ymin = -Inf, ymax = Inf, fill = p_above), alpha = 0.4) + 
    scale_fill_manual(values = c("white", "red"))

enter image description here