基于 ggplot 列表中是不是存在因子的颜色代码
Posted
技术标签:
【中文标题】基于 ggplot 列表中是不是存在因子的颜色代码【英文标题】:color code based on presence/absence of factor in a list with ggplot基于 ggplot 列表中是否存在因子的颜色代码 【发布时间】:2014-11-26 17:09:38 【问题描述】:我有一些基因组数据,其中包含为每个基因计算的各种统计数据(“BHTD”、“BHNS”)。还有一个列(“func.cat”)包含每个基因的功能分类;这些类别并不相互排斥,由数字 1-34 表示,即。一个基因可能属于多个功能类别,例如 1,5 和 7。我正在尝试编写一些代码,让我能够探索每个功能类别中的基因在整个数据集的分布中的位置。我遇到的问题与如何遍历 $func.cat 列中的列表有关(功能分类标记为 1-34,用“,”分隔)。
到目前为止我尝试过的是:
library(ggplot2)
dat$cat <- with(dat, factor(ifelse(func.cat == "4", 1, 0)))
(请注意,我会根据我希望查看的类别更改“4”)
我意识到“==”导致了我的问题,因为“1,4,5”不等于 4,但我不确定如何迭代该列表中的项目 - 这是我的根问题。
p1 <- ggplot(dat, aes(x=BHNS, y=BHTD)) +
geom_point(aes(colour = cat))
p1
这会生成一个图,将突出显示与归类为第 4 类的基因相对应的所有点,但仅显示第 4 类。我希望对任何已归类为第 4 类的基因进行着色,无论它是否具有也被归为另一类。
提前感谢您的任何建议!
这是数据的一个子集:
输入(数据) 结构(列表(BHTD = c(-2.407361658,-1.796755011,-2.033328407, -1.999300651,-1.705683372,-2.07316744,-2.14888815,-1.238415232, -1.964081372,-0.946236,-1.804456645,-1.117478076,-0.865610729, -2.014708065,-1.700170785,-2.066551154,-2.321719704,-1.382856082, -1.860020378,-1.938464139,-1.772410788,-1.98276263,-1.474897562, -2.341638838,-1.477241173,-1.997347068,-0.951587062,-1.472879561, -2.205905395,-1.573116753,NA,-1.793176812,-1.508295823,-1.411124368, -1.659159007,NA,-2.184113573,-1.753603939,-0.946572775,-2.230161692, -2.257687027,-2.052893551,-1.684633689,-1.723560773,-1.311466597, -0.878316233, -1.760067015, -0.991159868, -1.597358958, -1.68808286, -2.24992473,-1.384269004,-1.245007137,-0.733919882,-0.951587062, -1.716349956,-1.857569436,-2.03690476,-1.742918492,-1.652319766, -1.018325037,-1.237294825,-1.705582368,-2.251182699,-1.44607638, -1.86373038,-1.238915153,-1.999390778,-2.258813941,-2.307483301, -1.824001963,-2.046894346,-1.973018785,-1.822540934,-2.098850332, -1.622573473,-1.385826692,-0.13581749,-1.473072964,-1.912742974, -1.331192083,-1.35995547,-1.414638574,-2.3080906,-1.928952143, -1.73843242,-1.78263942,-2.045782556,-1.83695726,-1.782816678, -1.384301082,-1.738544771,-2.122562601,-1.008751008,NA,0.210106499, 不适用,-1.497540674,-1.175421047,-2.077668436,0,-2.392002104, -2.172277005,-2.248237481,-1.754777666,-1.205907566,-2.157860036, -2.372725867,-1.776418131,NA,-1.981646655,-1.83215468,-1.632970565, -1.796441664,-1.16749959,-2.097760427,-1.688111791,-1.091669998, -2.031922436,-1.609781944,-1.701092173,-1.741641383,-1.648047931, 0,不适用,-2.155091718,-2.19686492,-2.043064889,-1.900228157, -1.033211891,-1.741600124,-1.952787018,-1.635681652,-1.414146075, -1.59652266,-0.67250785,-1.741490676,-1.668132969,-1.547999618, -2.096369244,-1.585198776,-0.893064463,-1.720571973,-2.22165509, -0.87006502,-2.026357729,-1.903054158,-2.336779135,-1.804456645, -2.187397942,-1.738544771,NA,NA,-2.244769328,-1.443026239, -2.1075961,-1.284505684,-2.171199234,0,-1.832601732,NA, 不适用,-1.92361227,-1.404739557,-1.673782332,-1.630260976,-1.949121617, -1.535783949,-0.93437506,-1.682586839,-1.211290477,-1.837644381, -2.006840092,-2.094965703,-1.501569366,-1.686096428,-1.766893171, -1.025055976,-1.445292041,-1.804456645,-1.295447345,-2.333513471, -2.024633218,-1.945932896,-2.138973359,-1.192594283,-1.032891761, -2.274612038,-1.572404387,-1.157314923,-1.821517283,-0.951587062, -1.491063082,-1.262268477,-1.285825224,-2.125036752,-2.254193395, -2.025063005,-1.781927799,-1.817341356,-2.363964155,-1.324016132, -1.807867509,-1.28536312,-2.304611837,-1.805333968,-1.981011267, -1.148360279,-2.254965576,-1.832997391,-1.284505684,-1.36694829, -1.511236509,-2.199333133,-2.162070519,-1.613952632,-1.98074573, -1.937973938,-1.775626812,-1.110822415,-2.350956471,-0.798852174, -1.570167789,-1.205143435,-1.227387321,-1.870071641,-1.804119234, -1.852186861,-1.980559594,-1.910654458,-1.846696172,-0.851873121, -2.093777922,-2.009089876,-1.341030926,-1.714790638,-2.035487818, -1.655358562,-1.24566631,-2.268812805,-2.033792651,-1.964347513, -1.177587982,-1.014825692,-1.543347759,-1.982771035,-2.095046073, -2.396210518,-1.190624287,-1.980840471,-1.629812596,-2.075443039, -1.117986711,-1.284505684,-1.737984295,-1.909068715,-1.44607638, -0.13581749,-2.26199378,NA,-1.13387384,-1.775626812,-1.796501476, -1.98917241,-1.980551021,-1.804456645,-2.030319284,-1.450348529, -2.094406662,-2.473686253,-2.076634731,-1.507574766,-1.964339138, -1.459205458,-2.408551092,-2.150651491,-2.058411824,-2.073951488, -1.674678062,-2.066920278,-2.322477648,NA,NA,NA,-2.066255854, -1.589735284,-1.607268225,-2.548035167,NA,NA,-0.951587062, -1.700335988,-2.189638552,-1.659055578,-2.205270213,-1.472855439, -1.785739256,-1.627265507,-1.910645503,-2.050893789,NA,-0.062843564, -0.673444373,-1.091002703,-1.326825703,-1.050001441,-1.891635725, NA,NA,-1.494850186,-1.156519132,-1.999300651,-0.092286396, -0.183330333, -1.845843023, -1.640896562, -1.572404387, -1.860224553, -1.648638668,-1.872150192,-2.245024819,-1.737840368,-1.385826692, -1.981735368,-1.284505684,-2.14125006,-1.804456645,-0.973371896, -0.314443643,-1.715455449,-1.551622633,-1.673782332,-1.467606151, -1.910546588,-1.275308274,-1.78203764,-0.835133,-0.951587062, NA,NA,-1.467647481,-2.351522017,-1.63428397,-1.355853241, -2.110040041,-1.960573835,-2.230550217,-1.919743893,-1.064911257, -1.999122333,-1.717022526,-1.284505684,-2.195620712,-1.678699875, -0.951587062,-0.985647431,-1.760291205,不适用,不适用,-1.980847743, -1.546669986,-1.571405894,-1.210776173,-0.981913114,-1.507574766, -1.736694039,-1.540253602,-1.407640256,-1.697004898,-1.425404963, -0.952113105,-1.504651095,-0.664885437,-0.988878576,-1.963728301, -1.873625986,-2.375331349,-0.951587062,-1.714055933,-1.835483999, 0.531735976,-1.247617445,-1.597358958,-0.870980998,-1.910955616, -1.472767466,-1.909412827,-1.989851075,-1.928950806,-2.125207581, -0.702179857,NA,-1.258758453,-0.951587062,-1.910744418,-1.977408625, -2.185569218,-1.649306639,-1.67542209,-1.859870782,-1.688905215, -1.014897763,-2.098942661,-1.50114744,-1.507574766,-2.401004247, -1.630373071,-1.704144046,-2.341487878,-1.648352839,-1.449448077, -1.568042202,-1.056046556,-1.955734929,-1.326772791,-1.125210141, -1.263721543,-1.787741424,-1.631502453,-1.507574766,-1.832997391, -2.074270271,-1.720214873,-1.832601732,-1.981646655,-1.538923716, -2.074197994,-1.804456645,-2.219273959,-1.630260976,-2.095269031, -1.058597445,-1.103308726,-0.222433651,0.363131214,-1.316124286, -1.428135774,-1.13088371,-2.237049349,-1.212032461,-1.651632249, -1.321079056,-2.058330473,-1.908682716,-1.285163648,NA,-2.033320494, -1.561765422,-0.650534061,-1.54891411,-1.775629164,-1.16720697, -1.67466461,-2.11038659,-1.622183558,-0.805494207,NA,-1.284505684, -1.629812596, 0, -1.733554458, -1.471842151, -2.325564799, -1.931011616, -0.951587062,-1.674650606,-2.084007693,-0.13581749,-0.728334229, -0.697339499,-1.717022526,-1.84336553,-2.102771894,-1.613099192, -1.373005006,-1.404053215,-1.563983261,-1.240665527,-1.169474104, -0.499894742, -1.477392537, 0, -1.35617011, -1.552690092, -1.266997489, -1.800049126,-1.284505684,-1.738096391,-1.081560759,-0.353725082, -1.833482909,-1.820326999,-1.929571801,-1.445764205,-1.507261023, -1.029306695,-1.507418679,-1.28536312,-1.888950799,-0.925858686 ), BHNS = c(-0.536445534500279, -2.51590975211276, 0.975176838838268, 1.16762127648105、0.306137539516978、-1.74214520673759、-1.64717485268251、 -3.95922066144353,-3.39635293660797,-0.970330946311565,0.172619995428375, 2.48298883472192, 0.793974082613881, 0.501039140250149, 0.804337655982506, -3.30714604156556,-1.20707193264363,2.45797739273923,-1.59701672552781, -1.66914695085005, 0.0674012955390476, 1.62417842412305, -2.58203639235661, -2.12135486007817,不适用,1.90106289648472,不适用,-4.03524619467747, -1.22854600858282,-1.07477486644834,NA,-2.28606696929486, -2.66907956622894,-2.72803842459239,1.85426238301574,NA,-1.21282898286863, NA,2.2855102029862,0.277515630354603,-2.22927291562984,-0.408924692322956, 1.28800115719665, -3.38950127542429, 0.674326255003232, 0.100856586743738, -1.09361761185504,4.48642120653207,NA,-0.778285568839945, -1.61559599922626,1.11437504404848,不适用,2.36961492996114,不适用, -3.31657121257007,-1.26574933393628,-1.65527597153758,-1.43219956248087, -4.48337471597694,-0.38392378868904,2.18997939435775,-1.45440502892558, -0.498677781445362,NA,0.263936976427928,2.13996790558608, 2.02108542548667、2.04917825749504、0.0491580369030717、3.50533020548612、 0.691750405572648,-2.95240608774007,-3.02575598315416,-0.8637227975704, 0.759542644984083,-0.144270618649459,NA,-0.357833850766232, -2.35061857677948,0.846721856058998,-4.30457112149904,NA, 0.521384795227345,-2.68755642840854,0.301655700405515,-2.73328355695093, 1.81192054331534,-2.93795656056712,-4.54923476572377,-0.0705314386206745, 1.33535189698769, -0.814805548174206, 1.37244958099412, 1.06210539305522, -0.31272164063358,NA,NA,NA,-0.974460560578356,NA,-1.09691503978493, -1.16129610027415, 0.807081820419685, 0.219769515667387, NA, -2.11671131861236,-0.00825953442660159,-3.34135267869532,NA, -2.14077271733041,-1.53984154690997,-2.55464025816218,1.87699362206363, NA, 1.21608554754864, 2.37418777663669, 2.55877670413167, 0.802426328525387, -2.46322401518915,-2.68656498120426,-1.82904543820427,NA, NA,NA,-2.60297624909947,-3.67353291535057,-0.495574112777514, 2.03017007695609, -0.0124338742030449, 1.12209795456402, NA, 0.866863701855014, 3.7209135742554, 0.375208084011011, -1.45849683298558, -0.19123665984119, -0.188018140361373, 1.19734815284619, 0.00853725978565168, -0.730240497597164,NA,-4.96375079708713,1.19107712360479, -0.633787160803514, 0.499368742064575, 0.664122184333449, -0.103947741244196, -2.14739584713273, 1.74822462363828, 1.32687851322831, NA, NA, -0.616579186144451,-4.44411553936161,-2.84734557522817,-1.54029271130266, -0.292331969757492,NA,-1.66741119163565,NA,NA,-4.34294861371069, 不,不,1.84578349226819,-2.17212862641572,-0.548751753048712, 2.42407222784798, 1.98051589623148, 0.857017231589848, 1.02923101633178, 0.603889368418457,-0.155950277329299,-1.34170207835036,-3.12632714628269, 3.17556103654373、2.20650266094645、-5.13428281112186、-0.921178268637226、 -2.38181625620856, 0.160972414602853, 1.77091538996229, 4.63247625224926, -1.86579167976827,-4.69436622845745,1.63801459948559,-2.43515279935704, NA,NA,-3.78670320087694,NA,-0.288033414912556,NA,NA,0.0313451485705829, 0.966671153586328,-1.45301303679307,0.865396954575461,-1.04732750646681, -1.13862805823273,-3.33182170877955,-0.716789934481586,-1.46422431089161, 1.80608017491006,-0.819565581598858,-3.31489269127239,2.30213502005916, -1.19546412608738, 1.69987803022498, -1.50379312132888, 1.12150667071641, 3.29476953236869, -0.174091501660731, 0.0355055954410258, 1.68302476193471, -0.933232270264013,-1.20837727976455,-5.00113669120977,0.549514565656909, -1.22088165343573, -4.9131413352054, 0.249406010650101, -0.308297840121518, NA, 2.04846718184171, 0.560020743571995, -0.463120853229649, 1.40062718046321,-3.79755640951251,-1.88533639543057,1.57110593873126, -0.0299794333846566,1.22247764726521,NA,-2.03497022023155, -0.407010083622181,-2.37498324523997,NA,-0.349381507825823, -3.11946075987499, NA, -2.76573219788431, 3.29139667042278, 2.21696800111447, -0.086332960380396,0.836753244947099,-1.30865978206261,NA, -0.192919803126557,-5.35233490542491,-1.18731608332232,-3.69045479061667, -1.57841486746488, 1.18664265800922, -2.19679997569897, -2.0452403212891, 不适用,-1.45283826875268,不适用,4.56092709451823,-1.33156364802532, -4.39414824444583,-3.50663975811275,-0.994204002807067,0.465461528013715, 0.449957137212169,NA,-2.20473150634823,-2.43893778416633, -0.21859079543478,-2.49654949099443,-0.101281326767697,-1.83129379417679, -0.240462905250426,-1.0882958290627,-1.39034392349669,-1.14066661465865, 3.78358608669209, 1.02135372275077, -0.740754000333138, NA, NA, 不适用,-0.256965993799418,-2.49360159097253,-2.67415175736433, -1.75805187150358,NA,NA,NA,2.56779851815765,-1.43656049387778, 2.29452798322785, 0.862942019010368, 2.09421244256466, -2.00177427820928, 1.38403406309234, 0.740884397517659, 0.521458258281686, NA, NA, 3.45339851475839, NA, -3.26999440857194, 3.22431706477516, 1.12146738985331, NA,NA,-3.35074461633528,-4.77834384105443,0.861820227100293, -1.2056462223418,NA,-0.764953366530192,2.15999357109029,-3.15904866930723, -2.02972340363139,1.59312313935804,-0.690697193933604,-2.29058736067177, -5.13147313144091、1.07236945986908、0.108280351864803、-1.52505465628159、 -1.88392986532853,-0.913313498148003,3.45065659884515,-1.51081560834924, 不适用,3.00567498412257,不适用,2.29831624955477,-2.39939672938326, NA,NA,NA,NA,NA,NA,-5.3996538732268,-1.42752873210144, 0.0724852922370845, -0.0705972311145999, -1.61242654109663, 1.29582434107304, 1.25077987465511,-5.98015252408187,-3.61932820726323,-1.07781775258866, 0.286505399786564, -1.56602611718645, 0.399192492042174, 1.85548824455123, 不适用,-0.327392012404141,-1.97227490719439,不适用,不适用,-3.35557605502864, -3.290672107048,NA,3.48660286422453,-2.86599190336319,-0.581371472493803, -2.4331708832036,-2.51410746360557,0.288914981358372,-0.505196655524221, 1.05716585109166,NA,0.989822924708553,NA,0.0225311655108685, 1.62443349472587,-2.10875387227572,-0.587500068529866,NA, 0.425562050427503、1.22298892433114、-0.927706126888752、-0.409816745039093、 -4.06011308919161,-1.03787150354729,-0.526565521964921,1.80298858884609, -0.448432216080462,-3.45358148541365,-2.91502694941587,-0.991517726403897, 3.4514878901496,不适用,-1.12635167429918,不适用,-1.50038036462404, 2.32725535516867, 0.111782581861886, 0.470383741015466, -0.031317322019304, -0.718043104468153,-1.12920706646041,NA,2.01061929939003, 3.17413595098028,NA,0.190603542356822,-2.22133728253356,-1.06894473164686, -0.4330706698996, 1.01987682109617, -2.03059414210894, -1.7628873247019, -2.53482602530358,-1.21416018667071,-3.67031160485705,-1.52082991320074, 1.02860643265223,0.500315605421826,-0.716811146495161,NA, -0.195815384833931,-0.493307414686167,3.23812969301253,NA, -2.09109040458043,-3.1164773497449,-1.6343505259607,0.379609686590297, -0.0343891823066718, 1.10485260681212, -1.13046916214953, -0.69700197840047, -5.4193074181233,NA,NA,-4.42064283033986,NA,1.3717981277516, 0.727970491803614,-0.105635233546526,-2.39685477369571,5.1151037437021, -1.83992182746077,不适用,-1.40244592017941,不适用,0.486864923336782, 2.60269396800354,-3.362465559503,不适用,-3.98399191993587,不适用, -1.4572757861452,-1.19771254081594,-3.95432863178781,3.21854033879276, NA,NA,1.80796548853591,NA,-4.04344709825764,2.05959593320529, -0.161804833493671,-0.0552164788513592,NA,-2.86741460844131, 0.31984524711282, NA, NA, 4.74172559718353, 0.165786351005475, -4.36875963238208,-2.31924247294842,-0.935833349775849,NA, -3.28217665426608、1.32131433363562、1.71193300136779、-0.306511172657601、 -3.558816156159,-1.55714569371858,NA,1.18540625153828,-4.1986425447059, 4.05642691202438,-3.75551752685627,NA,1.39774357580828,-7.41748774382377, 0.490138847007415, -1.32879211731965, 0.529098504821919, -0.798177526937817, -0.845196892300538,-2.53754126606442,-2.63918687041961,NA, NA, 3.90070676740233, -1.36316117657554), func.cat = c("5,11,22", “5,22”,“5,9,22”,“1,9,28”,“5,11,22”,“5,8,22”,“4”,“4,9”, "5,9,25", "4,9", "4,9", "4,9,29", "7,15,18", "6,11,21,22,28,29,30 ", "6,11,21,28,30", "4,9,23", "4,9", "6,9,30", "6,9,30", "6,9,30", "1,9,28", "6,9", "6,9,21", "3,9,23", "1,9", "1,9", "1,9", "1 ", "1,9", "1,9", "9", "7,9,18", "2,9,19,27", "1,9,28", "2,9,19,27 ", “1,9”、“4,9”、“1,9,21”、“4,9”、“4,9”、“5,11,20”、“6,21”、“6,9” ,21", "7,9,13", "2,9,28", "7,9", "1,9,21", "4,9", "1,9", "4,8,9,23 ", “4,9,29”,“1,9,28”,“5,20”,“5,22”,“5”,“5,20”,“1”,“5,11,22”, "1,9", "1,11", "1,9", "7,9,17", "7,9,13", "3,4,8,9", "3,9,28 ", “7,9”,“6,21”,“7,9,19,27,28”,“7,9,15”,“7,9,15”,“5,9,22”, "4,9", "4,9,32", "1,27", "7,9,15", "4,9", "7,9,28", "1,1,6,21 ", “1”,“1,9”,“1,9”,“6,9”,“7,9,13”,“7,9,13,26”,“7,9,13”,“7 ,11,13", "7,11,13,26", "7,13", "2", "7,27,28", "4,9,29", "7,9,16", "4,9,26 ", “4,29”,“9”,“9”,“”,“7,9,27”,“2,9,10”,“2,9,10,19,27”,“1,9, 10", “2,9,10,27”,“4,11,29”,“4,9,26”,“1,9,30”,“5,9,20”,“1,28”, "4,9,26", "1,9", "8", "4,9,28", "7,9,19", "4,11,17,23", "4,9,17 ", “4,9,15”,“4,9,28”,“4,9,29”,“6,9,21”,“7,11,15,18”,“2,19,27”, “5,20”、“1,9”、“1,9”、“1,9”、“”、“7,25”、“3,9”、“3,11,17”、“4”、 9", "2,9,28,33", "7,9,19", "2,9,19", "7,9,13", "7,9", "3,9,28", "6 ,9,21", “7,9,27”,“7,9”,“1,9”,“7,9,17,23”,“1,9”,“1,9,28”,“1,9,22” ", "4,9,26", "6,9,21", "2,9,27", "2,9,27", "7,9,13", "7,9,19,27,28 ", “7,9,13,28”,“1”,“9”,“9”,“6,30”,“2,9,19”,“7,13”,“7,9,13”, “1,7,9”,“1”,“6,9,21”,“9”,“9”,“7,13,33”,“7,9,13”,“1,9,28 ", "1,19", "6,9,21", "2,9,19,27", "3", "3,9", "3,9,10,27", "3,9,10 ,27", “3,9,10,27”,“3,27”,“4,9,10,27”,“3,9,27”,“4,9,10”,“4,9,10”, “1,9”、“4,9”、“4,9”、“4,29”、“1,9,28”、“5,9,21”、“7,9,19”、“1 ,9", "1,9", "3,7", "7,9,28", "4,9", "7,11", "1,9,29", "4,9,17", "1 ,4,9,29", “4”,“1,9”,“4,9,32”,“6,9,21,30”,“6,9”,“7,9,13”,“7,9,25”, “4,10”,“4,9”,“1,9”,“4,9,28”,“4”,“4,9,10,29”,“4,11,28”, “4,9,28”,“1,9”,“7,11,28”,“1,9”,“1,9”,“7,13”,“6,9,18”,“7 ,9,13", "2,9,19,27", "2,9,19", "7,9,10,19", "7,9,19", "4,8,10,28", "4,9 ", “7,9,19”,“2”,“2,19”,“7,13”,“7,11,27,28”,“4,23”,“4,11”, "4,11", "7,11,19", "4,9,28", "2,9,28", "2,9,19", "6,9,21", "5,9 ,16", “7,13”、“4,9”、“4”、“4,4,11”、“4,9”、“6,9,21”、“3,9”、“3,9,28 ", “7,9,19”,“2,9,19,27,28,33”,“2,9,19”,“2,19”,“7,9,28”,“4,9”, "7,13", "7,13", "7,9,10", "1,9", "3,9,25", "7,9,13", "7,9,26,28 ", "7,9,18", "7,9,18", "9", "1,9", "1,9", "1,9,29", "6,9,18,21,30 ", "4,9,26", "3,9", "1,9", "1,9", "4,9,26", "7,9,15,27", "4,9,26 ", "1,9,14", "1,9,22", "2,9,19,27", "2,9,19", "1,9", "6,9,21", "1 ,9,15", “6”,“1,9,29”,“3,3,9”,“8”,“8”,“8”,“2,9,27”,“4,11,25”,“4” ,11", “4,11”,“11”,“11”,“4”,“4,9,29”,“4,11”,“4,11”,“7,11,25”, “4,11”、“1,9”、“7,9,28”、“1,9,29”、“7,9,26”、“9”、“1,9”、“1,9” ", "3,9", "3,9,28", "6,9,21", "7,9,19,27,28", "9", "9,24", "7,9,13 ", “1,9”、“4,19”、“4,29”、“1,9”、“1,11”、“1,11,25”、“1,9”、“4,9”、 "7,9,17", "7,9,27", "7,9,13", "4,9,26", "7,9,25", "4,9", "7,9 ,16", “7,9,23”,“1,9,29”,“6,9,21,26”,“9”,“9,10”,“7,27”,“6,9,21”, “1,9”,“1,9,21”,“7,9,28”,“1,9”,“1,9”,“7,11,23”,“11”,“9”, “7,11,15”,“7,11,13”,“6,21,30”,“1,9”,“4,9”,“4,9,28”,“4,28”, “4,9”,“1,9,28”,“4,9,15”,“4,11”,“6,9”,“1,9”,“3,11,25”,“3 ,11,25", “3,11,25”,“3,9,21”,“9”,“24”,“1,9,27”,“7,11”,“1”,“4,9,28”, “1,9”、“4,9”、“4,9,26”、“7,17”、“4,29”、“1,9,17”、“1,9,29”、“1 ,9", “1,9,28”,“7,9,29”,“7,28”,“1,9,28”,“1,11,25”,“7,9,13”,“7,13” ", "7,9", "1,9,25", "6,9,21", "1", "4,9,29", "7,9,20", "1,9", "7 ,9,16", “4,11”,“3,11,25”,“7,9,13,18”,“6,9,21,28,30”,“1,9”,“8,9”, “7,9,16”,“1,9,26”,“7,9,15”,“7,9,13”,“9”,“4,9”,“1,9”,“1 ,9", “1,9”、“4”、“4,11,32”、“2,11,19”、“4,9”、“4,9,28”、“4,9”、“2,11” ,19,27", "2,9,27,33", "1,9,28", "7,9,13", "7,13,28", "7,9,13", "6,11,21,28" ,30", "4,11,15,30", "4,11", "5,9,22,28", "7,10,18", "7,11,15", "7,11,18", “7,11,18”,“4,9,28”,“4,9”,“4,9”,“1,9,28”,“7,11,18”,“7,11”, “1,9”,“4,11,26”,“4”,“5,10,22”,“7,9”,“5,11”,“1,11”,“4,9”, “3,9”,“1,9,29”,“1,9,28”,“4,9”,“2,19”,“2,9,19”,“7,9,18”, “7,9,19,27,28”,“3,11,25”,“1,9”,“9”,“1,9”,“5,9”,“5,9,21”, “4,9,29”,“2,9,23”,“1,9”,“1,28”,“4,11,28”,“4,9”,“6,9,21”, "9", "1,9", "1,9", "3", "2,3,3,9,19", "7,9,15", "7,9,13", "1 ,9", “1”,“4,9”,“7,11,13”,“4,9”,“1,9,29”,“6,21,28”,“1,9,19”, "7,13", "2,9,19", "2,9,23,33", "1,18", "1,2,9,10,18,23,33", "1,9 ", “6,9,21”,“4,9,29”,“6,9,21”,“4,9”,“4,9”,“4,9”,“7,9,16”, "4,11", "7,9,28", "1,9", "4,9", "4,9,29", "7,9,28", "6,9,17,21 ", “7,23”、“1,9”、“4,28”、“7,17”、“6,9,10”、“6,9”、“1”、“1,7,9,15 ", "1,9")), .Names = c("BHTD", "BHNS", "func.cat"), class= "data.frame", row.names = c(NA, 500L))
【问题讨论】:
如何定义多个级别的cat
?您不必使用ifelse
进行操作。只需定义变量,然后为每个满足的条件分配数值。之后您可以将其转换为因子并在ggplot
中使用。
感谢 SimonG 的建议。你能举个例子说明你是如何编码的吗?恐怕我不知道如何定义多层次的猫。
我才意识到我误解了你的问题。我会发布一个答案。
【参考方案1】:
所以这里有一个稍微不同的方法,使用dat
的前十行。
dat$cat <- strsplit(dat$func.cat,",")
library(ggplot2)
ggplot(dat) +
geom_point(aes(x=BHTD,y=BHNS,color=sapply(cat,function(cat) "4" %in% cat)),
size=3)+
scale_color_discrete("Fun.Cat=4")
此代码创建一个列 $cat
作为向量列表,每个向量包含该行中表示的基因的所有功能类别。使用
color=sapply(cat,function(cat) "4" %in% cat)
测试cat
的每个元素(dat
的每一行)。如果该行中的向量包含“4”,color=TRUE
。这可能有一点优势,您不必为要突出显示的每个类别在整个表格中 grep
。
【讨论】:
谢谢!这正是我一直在寻找的——这也将在未来的努力中证明是有用的!【参考方案2】:我想这就是你的意思。我使用 for 循环和 grep
创建在 ggplot
中使用的类别。据我了解,您希望每次迭代匹配一个 func.cat
的子字符串(例如,"1"
)。
我根据你的创建了一个最小的例子:
library(ggplot2)
c1 <- c("1","2","2")
c2 <- c("2","","3")
dat <- data.frame(BHNS=1:3,BHTD=1:3,func.cat=paste(c1,c2,sep=","))
for(ii in c("1","3"))
dat$new.cat <- 0
dat <- within(dat,
new.cat[grep(gsub("i",ii,"^i$|^i,|,i$|,i,"),func.cat)] <- 1
)
p1 <- ggplot(dat, aes(x=BHNS, y=BHTD)) +
geom_point(aes(colour = factor(new.cat)))
png(file=paste("plot_",ii,".png",sep=""))
print(p1)
dev.off()
希望这会有所帮助。
干杯!
编辑:更新了对 grep
的调用以不匹配不需要的类别。
【讨论】:
这几乎可以工作 - 使用 grep 的问题是我有功能类别 1-34 所以如果我 grep "4" 我也会选择 "14"、"24" 和 "34" ”。是否有与字符/因素匹配的 grep 替代方法?非常感谢您的帮助! 好点!我更新了grep
调用,假设func.cat
仅包含数字或逗号分隔的条目。以上是关于基于 ggplot 列表中是不是存在因子的颜色代码的主要内容,如果未能解决你的问题,请参考以下文章
ggplot 手动图例 scale_fill_manual 用于单独的颜色因子
R语言ggplot2可视化:使用ggplot2按照热力图的方式显示全是分类变量的dataframe数据并自定义因子(factor)的图例颜色legend