向相关图添加线

问题描述

我正在尝试创建一个由药物特性决定的+/-氧浓度下的药物浓度之间的相关图。到目前为止,它看起来不错,但是我想添加连接点的线,但是我似乎还不知道该怎么做。 stat_cor的参数没有帮助。

```ggplot(df,mapping = aes(Oxygen,concentration)) + geom_point() + facet_wrap(~Drug,scales = 'free') + stat_cor()```

我也尝试了ggscatter,但没有成功。

```ggscatter(df,x = 'Oxygen',y = 'concentration',add = 'reg.line') + facet_wrap(~Drug,scales = 'free') + stat_cor()```

请参见下面的数据框:

structure(list(Sample = c(1,2,3,4,5,6,43,46,47,83,84,86,87,125,126,127,129,165,167,168,169,170,206,207,209,210,211,247,248,249,251,252,288,289,290,291,293,329,330,331,332,333,370,371,372,373,374,375,411,412,413,414,415,416,452,453,454,455,456,457,493,494,495,497,498,534,535,536,537,538,539,575,576,577,578,579,580,616,618,619,620,621,657,658,659,660,662,698,699,700,701,702,703,739,740,741,742,743,744,780,781,782,783,784,785,821,822,823,824,825,826,862,863,864,865,866,867,905,906,907,908,944,945,947,948,985,986,987,988,990,1026,1027,1028,1029,1030,1031,1067,1068,1069,1070,1071,1072,1108,1109,1110,1111,1112,1113,1149,1150,1151,1152,1153,1154,1190,1192,1193,1194,1195,1231,1232,1233,1234,1236,1272,1273,1274,1275,1276,1277,1313,1314,1315,1316,1317,1318,1354,1355,1357,1358,1359,1395,1396,1397,1398,1399,1400,1436,1437,1438,1439,1440,1441,1477,1478,1479,1480,1481,1482,1518,1519,1520,1521,1522,1523,1559,1560,1561,1562,1563,1564,1600,1601,1602,1603,1604,1605,1641,1642,1643,1644,1645,1646,1682,1683,1684,1685,1686,1687,1723,1724,1725,1726,1727,1728,1764,1767,1768,1769,1806,1807,1846,1848,1849,1850,1887,1888,1890,1891,1892,1928,1929,1930,1932,1933,1969,1970,1971,1972,1974,2010,2011,2012,2013,2014,2051,2052,2053,2054,2055,2056,2092,2093,2094,2095,2096,2097,2133,2134,2135,2136,2137,2138,2174,2175,2176,2177,2178,2179,2215,2217,2218,2219,2220,2256,2257,2258,2259,2260,2297,2298,2299,2300,2301,2302,2338,2340,2341,2342,2343,2379,2380,2381,2382,2384,2420,2421,2422,2423,2424,2425,2461,2462,2463,2464,2465,2466,2502,2503,2504,2505,2506,2507,2543,2544,2545,2546,2547,2548,2584,2585,2586,2587,2588,2589),Drug = c("Ab1","Ab2","Ab3","Ab4","Ab5","Ab6","Ab1","Ab6"),concentration = c(538,40,7300,530,230,50,140,215,440,540,120,3850,12340,5090,3190,610,6360,520,190,500,30,460,350,6070,150,6670,220,3820,3860,11960,5350,2000,2570,1670,11390,3950,1170,810,3240,2610,11840,4400,1580,1900,320,6150,300,110,2560,1660,11910,1120,870,3830,3610,12320,5440,2040,2860,2810,1840,11250,2730,1040,1080,3430,12440,5160,1770,2230,400,90,3480,490,3230,270,550,3570,430,3010,240,130,690,4050,200,80,3530,160,260,680,70,1240,1430,3800,4700,560,2770,3500,310,1230,3000,5000,720,890,910,860,2930,4540,480,590,1410,1550,3990,5140,900,1710,4060,1290,2830,3390,470,1650,1820,4220,5430,1470,2630,380,280,1860,3720,4020,2920,640,4650,390,1790,880,4320,830,970,3740,2880,4080,760,1450,5310,920,1360,60,5190,800,1370,100,5100,1250,770,5530,1100,1390,1540,5720,1260,4140,710,20,1000,3710,180,2030,3590,820,3450,4910,3910,410,4210,10,10),Oxygen = c("+","+","-","-"
    ),Donor = c("D1","D1","D2","D3","D4","D5","D6","D7","D8","D8"),Illness = c("pneumonia","pneumonia","gastro","TB","lyme","lyme")),row.names = c(NA,-345L
    ),class = c("tbl_df","tbl","data.frame"))

解决方法

也许您正在寻找这个:

#Code 1
ggplot(df,mapping = aes(Oxygen,concentration)) +
  geom_point() +
  geom_line()+
  facet_wrap(~Drug,scales = 'free') +
  stat_cor()
#Code 2
ggplot(df,concentration,group=1)) +
  geom_point() +
  geom_line()+
  facet_wrap(~Drug,scales = 'free') +
  stat_cor()

#Code 3
ggplot(df,group=Drug)) +
  geom_point() +
  geom_path()+
  facet_wrap(~Drug,scales = 'free') +
  stat_cor()

输出:

enter image description here

enter image description here

enter image description here