添加第二条y轴作为第一条x和y轴的公式

时间:2019-07-15 10:49:06

标签: r ggplot2

我有以下数据:

  ClusterID AvgGenes nCoreGenes    Ratio
     20001     1941       1572 0.809892
     20005     1599       1374 0.859287
     20008     2017       1712 0.848785
     20009     1808       1590 0.879425
     20013     1823       1469 0.805815
     20015     2056       1677 0.815661
     20019     2135       1783 0.835129
     20020     3152       2625 0.832805
     20026     2028       1586 0.782051
     20028     1835       1420 0.773842
     20030     2885       2189 0.758752
     20031     1772       1485 0.838036
     20032     1722       1473 0.855401
     20034     1801       1459 0.810105
     20035     1677       1339 0.798450
     20042     2193       1651 0.752850
     20047     1747       1345 0.769891
     20049     1306       1008 0.771822
     20051     1738       1358 0.781358
     20052     1552       1188 0.765464
     20062     2179       1509 0.692520
     20065     2047       1894 0.925256
     20074     1948       1568 0.804928
     20088     2588       2192 0.846986
     20103     1916       1341 0.699896
     20109     2511       2190 0.872162
     20117     1668       1278 0.766187
     20162     1936       1601 0.826963
     20167     2068       1856 0.897485
     20168     4375       3992 0.912457
     20170     3961       3252 0.821005
     20190     2327       2013 0.865062
     20196     3350       2522 0.752836
     20198     3028       2302 0.760238
     20207     1522       1241 0.815375
     20208     1791       1546 0.863205
     20215     3013       1853 0.615002
     20219     2803       2043 0.728862
     20225     4604       2931 0.636620
     20247     1927       1567 0.813181
     20248     2510       1732 0.690040
     20251     2252       1674 0.743339
     20279     2843       1775 0.624340
     20293     1611       1245 0.772812
     20313     2277       1914 0.840580
     20314     2320       1915 0.825431
     20318     2201       1762 0.800545
     20320     2287       1943 0.849585
     20321     2060       1645 0.798544
     20323     2242       1524 0.679750
     20327     2132       1845 0.865385
     20328     1685       1402 0.832047
     20329     2393       1727 0.721688
     20341     2190       1729 0.789498
     20368     3906       2991 0.765745
     20370     3245       2325 0.716487
     20373     2608       1935 0.741948
     20374     3632       2380 0.655286
     20388     1787       1435 0.803022
     20408     1506       1262 0.837981
     20423     1979       1428 0.721577
     20433     2452       1646 0.671289
     20459     2118       1649 0.778565
     20462     1778       1496 0.841395
     20478     1653       1447 0.875378
     20492     2709       1895 0.699520
     20494     2686       1773 0.660089
     20498     2676       1909 0.713378
     20508     1425       1092 0.766316
     20517     2461       1983 0.805770
     20548     2752       2059 0.748183
     20565     2239       1764 0.787852
     20566     2368       1882 0.794764
     20569     2285       1877 0.821444
     20572     2179       1703 0.781551
     20573     1609       1355 0.842138
     20577     1753       1379 0.786651
     20579     1786       1426 0.798432
     20589     1811       1239 0.684152
     20600     2293       1822 0.794592
     20650     1693       1422 0.839929
     20677     1904       1485 0.779937
     20729     1680       1362 0.810714
     20742     2210       1855 0.839367
     20744     1583       1372 0.866709
     20746     2087       1743 0.835170
     20750     1859       1418 0.762776
     20753     1701       1496 0.879483
     20758     1480       1169 0.789865
     20759     1839       1406 0.764546
     20772     2068       1786 0.863636
     20773     2321       2024 0.872038
     20775     2528       2012 0.795886
     20784     1869       1592 0.851792
     20788     1843       1516 0.822572
     20809     1541       1352 0.877352
     20811     1569       1346 0.857871
     20824     1594       1323 0.829987
     20836     2287       1688 0.738085
     20857     2252       1704 0.756661
     20890     1884       1340 0.711253
     20903     1681       1404 0.835217
     20966     1826       1455 0.796824
     20967     1877       1605 0.855088
     20990     2125       1605 0.755294
     21002     1743       1345 0.771658
     21027     1866       1504 0.806002
     21047     2866       2191 0.764480
     21049     2163       1596 0.737864
     21059     2298       1847 0.803742
     21085     1640       1490 0.908537
     21258     3002       1950 0.649567
     21325     2945       2117 0.718846
     21326     2343       1996 0.851899
     21348     2362       1809 0.765876
     21370     2313       1553 0.671422
     21384     1932       1383 0.715839
     21405     1948       1398 0.717659
     21477     1852       1538 0.830454
     21584     2514       1838 0.731106
     21586     1247        910 0.729751
     21734     1619       1452 0.896850
     21818     1593       1363 0.855618
     21826     2688       2009 0.747396
     21845     2595       1854 0.714451
     21889     1678       1285 0.765793
     22085     1718       1314 0.764843
     22153     1290       1139 0.882946
     22347     2356       1629 0.691426
     22359     2170       1552 0.715207
     22396     1648       1337 0.811286

我想使用AvgGenes作为我的x轴,使用nCoreGenes作为我的主要y轴。另外,我想添加第二个y轴,用于nCoreGenes / AvgGenes * 100(pCoreGenes)的比率。但是,我找不到正确的公式:ggplot2中用于scale_y_continuous(sec.axis())的y轴/ x轴* 100。

cluster2core$pCoreGenes <- cluster2core$Ratio*100
g6 <- ggplot(cluster2core, aes(AvgGenes, nCoreGenes))
g6 <- g6 + geom_point(aes(y = nCoreGenes)) + geom_smooth(method = lm)
g6 <- g6 + geom_line(aes(y = pCoreGenes))
g6 <- g6 + labs(y = "Number of core genes", x = "Average number of genes")
#g6 <- g6 + scale_y_continuous(sec.axis = sec_axis())

比率%的平均值为78.7,因此我希望得到一条水平线,表明平均而言,基因组具有78%的核心基因。

1 个答案:

答案 0 :(得分:0)

在ggplot中使用辅助轴需要作弊。您需要假装您的次要y轴数据与主要y轴数据在同一范围内,因此请相应地缩放它。乘以100是不够的,因为您希望数据在1000左右的范围内。乘以4000应该可以到达那里。

然后,您需要反转轴的过程,并为sec_axis指定一个参数。通常,您可以将其除以4000,但是由于需要百分比,请除以40:

ggplot(df, aes(x=AvgGenes, y=nCoreGenes)) + geom_point() + 
   geom_smooth(method=lm) +
   geom_line(aes(y=Ratio*4000)) + 
   scale_y_continuous(sec.axis=sec_axis( ~ . / 40))

此外,由于它是从geom_point调用中的美学继承而来的,因此无需在ggplot()中指定美学。

enter image description here

相关问题