与ggplot2尝试使用space =“free_x”的facet小提琴情节

时间:2013-09-19 17:19:34

标签: r ggplot2

我试图在不同的方面制作一套小提琴情节,我有两个分类变量;一个(Thres),我用来将数据分成不同的方面和第二个(NSNP),我在每个方面使用它来为每个条件绘制单独的小提琴图。

我设法很好地生成了分面图,但是有两个分类变量的组合是空的,在某些方面产生了差距。

当我运行下面的代码时,我得到7个方面,在前6个方面我没有任何值在0和底部方面我没有1-3的任何值所以有空白图表。我想要做的是改变小平面的y轴(或这些图被翻转时的x),以便不出现间隙。我认为这可以用scale =“free_x”,然后space =“free_x会将小平面的大小调整为小提琴小块数量的比例。但是这不起作用。

以下是用于绘制此图表和数据子集的代码:

mydat3<-structure(list(values = c(0.095859549802147, 0.0464175342967273, 
0.0645501085582447, 0.0561315693403545, 0.0877787999642726, 0.0642197694344365, 
0.0572295087554683, 0.0443288995522364, 0.222621590122593, 0.0740180403310751, 
0.0374859692788302, 0.0649844262623869, 0.0477535429985445, 0.0578778559220613, 
0.124747971382994, 0.0420499183212924, 0.0608272849856844, 0.109997854535794, 
0.040810873046286, 0.0355291667607834, 0.0423016408325608, 0.0640249415590838, 
0.0631025124242698, 0.0411585961572307, 0.136957487216637, 0.0387811092625465, 
0.0718037442496459, 0.116606405377969, 0.0528890074588327, 0.0499231878553924, 
0.0534992620063799, 0.0947230498828188, 0.0883366814920039, 0.11384526623344, 
0.0687251526902865, 0.0357594877478181, 0.0375677751221073, 0.119793765548604, 
0.0500635079856242, 0.0757449093875329, 0.0463374386715252, 0.0583236969356681, 
0.0604792057680754, 0.0434423378024008, 0.052136262404202, 0.0430064406231872, 
0.0444112355025033, 0.0418896505775744, 0.0757469496352429, 0.0758996304800777, 
0.0428211439773155, 0.0492493504318606, 0.0460689699781013, 0.0350046476282005, 
0.0887832511289573, 0.0356368589361509, 0.0688351644690967, 0.0600420393848446, 
0.0477733282447943, 0.0502100159694551, 0.0787171457052969, 0.0751271656818236, 
0.0531879475355363, 0.0382722034225811, 0.035657219042404, 0.0389456460835204, 
0.0829960042140691, 0.0402727425198968, 0.0525990459535811, 0.0947979557634847, 
0.095130322005081, 0.0551447680805923, 0.055276571931073, 0.0556015731060283, 
0.0527454033481276, 0.0876700035871922, 0.0391599523515732, 0.0680024630785036, 
0.0905942423608025, 0.0639705989556369, 0.0590999197252122, 0.0814456268099261, 
0.0634189563885887, 0.0990217355953055, 0.0797654619911409, 0.045421303951918, 
0.0517178455574364, 0.0386867009753076, 0.0850274903685002, 0.0445797895112061, 
0.116564307393344, 0.038772531323172, 0.0621098771850116, 0.0348079128242265, 
0.10267105504507, 0.0715667696309822, 0.0438896766810641, 0.066568373657306, 
0.144406789469503, 0.0511856755979947, 0.0591439320668585, 0.0639941302879705, 
0.0564336074213047, 0.0504141098122474, 0.0532264855281964, 0.118145668980743, 
0.0491686829848695, 0.0625301117449126, 0.0353124997786777, 0.0756394390521888, 
0.0440626375469502, 0.0739048800419004, 0.0755801825925368, 0.0479095569739974, 
0.0366314438481509, 0.0615842885408238, 0.0363103805418523, 0.10642984356638, 
0.0764325237077806, 0.0509339599422938, 0.0393960284693845, 0.0373715456967969, 
0.0573340180311538, 0.0758040570580679, 0.0397290988766974, 0.0793429331504033, 
0.124620160674225, 0.0732741719432426, 0.0370684018084896, 0.0476385737967006, 
0.0597815734883134, 0.0562108125435101, 0.158564982549544, 0.0617660377137849, 
0.071804618279066, 0.0368174737649545, 0.0647498178876398, 0.0732395320154573, 
0.0814831305316639, 0.0612397928970058, 0.0959496476808134, 0.0459966223425876, 
0.0541987769424167, 0.0860931786221091, 0.0736220596207053, 0.0363665246832541, 
0.0874935850613301, 0.0525685223267564, 0.0529802277676731, 0.0652507657024711, 
0.0670781253098693, 0.0733998516445914, 0.0891335745005303, 0.0358838922328816, 
0.0353689609691281, 0.0955191513612327, 0.0350167098226862, 0.0628618588604291, 
0.0894348094605337, 0.0775709383187923, 0.0564545046951287, 0.0590306428878671, 
0.0431026022687549, 0.0352473914227291, 0.0824650449374406, 0.0433132254279204, 
0.0903319052544356, 0.0725367709023878, 0.074030363918439, 0.0358618642485878, 
0.0528328796271687, 0.0500283680499483, 0.0449320705005457, 0.0614653031170808, 
0.040769843945677, 0.143428459338737, 0.0698243152427977, 0.0477887900201451, 
0.0747315421364465, 0.119311284945825, 0.0352643913256084, 0.0784914420073424, 
0.0491393826410522, 0.0355805581325951, 0.15570636077347, 0.035497104091947, 
0.0574371100825385, 0.0353842603034302, 0.037352604229049, 0.0391887921986735, 
0.0797965714402714, 0.0620930251697114, 0.0415559243262849, 0.0692348409636982, 
0.0481644739268709, 0.0379875120963265, 0.0642578402415982, 0.0663484169892082, 
0.0546726909263926, 0.108776260865374), NSNP = structure(c(2L, 
3L, 4L, 3L, 3L, 2L, 4L, 2L, 4L, 3L, 2L, 2L, 2L, 4L, 4L, 3L, 2L, 
2L, 2L, 4L, 2L, 4L, 2L, 2L, 4L, 2L, 3L, 2L, 4L, 2L, 2L, 4L, 4L, 
4L, 3L, 1L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 2L, 2L, 
2L, 4L, 2L, 2L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 2L, 3L, 2L, 1L, 4L, 
4L, 2L, 4L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 2L, 4L, 2L, 
4L, 3L, 2L, 3L, 4L, 4L, 4L, 3L, 2L, 3L, 4L, 3L, 2L, 3L, 2L, 3L, 
3L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 4L, 2L, 4L, 3L, 3L, 3L, 
2L, 1L, 4L, 3L, 2L, 4L, 3L, 2L, 2L, 3L, 2L, 1L, 3L, 3L, 2L, 2L, 
2L, 2L, 3L, 2L, 2L, 3L, 4L, 2L, 2L, 4L, 4L, 2L, 3L, 2L, 3L, 3L, 
2L, 2L, 2L, 4L, 2L, 2L, 3L, 3L, 4L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 
3L, 2L, 3L, 2L, 3L, 3L,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"0.88", "0.9", "All"), class = "factor")), .Names = c("values", 
"NSNP", "Thres"), row.names = c(417387L, 987453L, 1120252L, 1589400L, 
971693L, 615757L, 847346L, 410925L, 1608540L, 1014470L, 1205725L, 
685241L, 663860L, 544104L, 1352348L, 978475L, 98571L, 1195361L, 
143201L, 829504L, 641176L, 341040L, 169825L, 425417L, 276036L, 
408672L, 720953L, 1191303L, 286624L, 903611L, 622910L, 327340L, 
790653L, 1396838L, 718503L, 82077L, 88491L, 749871L, 1454937L, 
121248L, 384249L, 653470L, 360233L, 1494330L, 239841L, 864825L, 
1074417L, 410545L, 920891L, 400393L, 326761L, 641876L, 401975L, 
1653154L, 822327L, 1053389L, 1240346L, 794594L, 486644L, 768056L, 
621590L, 1035600L, 394602L, 28528L, 1679232L, 1629924L, 919937L, 
332013L, 1414295L, 1317675L, 1473269L, 54407L, 1004545L, 992729L, 
1239328L, 550396L, 1088133L, 335236L, 670946L, 779868L, 920505L, 
1387472L, 1036523L, 909955L, 1006071L, 601045L, 1408820L, 598883L, 
1523182L, 1427672L, 1560314L, 1332208L, 1276817L, 880601L, 693423L, 
1157790L, 467721L, 768804L, 859699L, 1209937L, 1430736L, 372811L, 
394230L, 1369202L, 1054401L, 941276L, 877757L, 810209L, 385949L, 
284348L, 1317120L, 495510L, 1544732L, 652690L, 86507L, 846850L, 
217125L, 131391L, 286462L, 992319L, 151147L, 1226911L, 1305568L, 
632777L, 77445L, 252553L, 1288795L, 1467461L, 145905L, 1496846L, 
94403L, 234111L, 1177327L, 888364L, 737615L, 861742L, 398363L, 
356937L, 1127527L, 1337220L, 655199L, 1529402L, 1216012L, 756415L, 
1257172L, 637334L, 1202243L, 1212484L, 1342293L, 1411642L, 882076L, 
1246269L, 736798L, 1385152L, 452031L, 1239335L, 113083L, 1044007L, 
254527L, 875129L, 605332L, 1023917L, 644542L, 1574340L, 1192809L, 
180477L, 1010220L, 692625L, 298466L, 59057L, 1521617L, 1034449L, 
738916L, 1179635L, 1349147L, 337861L, 674869L, 995497L, 1230818L, 
1588357L, 901193L, 444274L, 397085L, 1018475L, 953867L, 520349L, 
37323L, 1070478L, 51129L, 527L, 160676L, 994120L, 1493324L, 19218L, 
873355L, 1362670L, 869612L, 817322L, 209286L, 788913L), class = "data.frame")

library(ggplot2)

ggplot(mydat3, aes(x=NSNP, y=values)) +geom_violin(aes(fill = NSNP)) + facet_grid(Thres ~ ., scale = "free_x", space = "free") + coord_flip() +opts(strip.text.y = theme_text()) + scale_x_discrete(breaks = c(1,2,3,4), labels = c("0", "1", "2", "3"))+ xlab("Up to N SNPs adjusted") + ylab("Standard Error of Beta Coefficient")+opts(legend.position = "none")

由于

0 个答案:

没有答案