簡體   English   中英

在散點圖中繪制線而不是點

[英]Plot Lines instead of points in scatterplot

dput(AM_cost_resorb[,1]) c(176.01685819061, 21.6704613594849, 19.007554742708, 50.1865574864131, 17.6174002411188, 40.2758022496774, 11.0963214407251, 1249.94375253114, 694.894678288085, 339.786950220117, 42.1452961176151, 220.352895161601, 19.6303352674776, 9.10350287678884, 10.6222946396451, 44.1984352318898, 21.8069112975004, 42.1237630342764, 22.7551891190248, 12.9587850506626, 12.0207189111152, 20.2704921282476, 13.3441156357956, 9.13092569988769, 1781.08346869568, 71.2690023512206, 80.2376892286713, 344.114362037227, 208.830841645638, 91.1778810401913, 2220.0120768657, 41.4820962277111, 16.5730025748281, 32.30173229022, 108.703930214512, 51.6770035143256, 709.071405759588, 87.9618878732223, 10.4198968123037, 34.4951840238729, 57.8603720445067, 72.3289197551429, 30.2366643066749, 23.8696161364716, 270.014690419247, 13.8170113452005, 39.5159584479013, 27.764841260433, 18.0311836472615, 40.5709477295999, 33.1888820958952, 9.03112843931787, 4.63738971549635, 12.7591169313099, 4.79988 94219979, 8.93458248803248, 7.33904760386628, 12.0940344070925, 7.17364602165948, 6.514191844409, 9.69911157978057, 6.57874454980745, 7.90556524435596)

dput(AM_leafP*1000) c(0.02840637, 0.230728821, 0.2630533, 0.099628272, 0.28381032, 0.12414402, 0.45059978, 0.00400018, 0.00719533500000001, 0.014715103086687, 0.118637201789886, 0.022690875, 0.254707825, 0.54923913, 0.470708088, 0.113126176837872, 0.22928510745, 0.118697847481752, 0.219730100850697, 0.38583864, 0.4159485, 0.24666396693114, 0.374696992776912, 0.547589605297248, 0.00280728, 0.070156727820596, 0.062314855376136, 0.01453005323695, 0.02394282358199, 0.0548378613646, 0.00225224, 0.120533928, 0.301695482, 0.15479046, 0.045996497, 0.096754836, 0.00705147600000001, 0.0568428, 0.47985120103071, 0.14494777, 0.08641493, 0.069128642, 0.165362156, 0.20947132, 0.018517511, 0.36187275779699, 0.126531158458224, 0.180083867690804, 0.277297380904852, 0.1232408972382, 0.15065285976048, 0.55364067, 1.07819275643191, 0.39187665, 1.04169066418176, 0.55962324, 0.68128731, 0.41342697, 0.69699564, 0.76755492, 0.515511133042674, 0.760023430328564, 0.632465844687028)

我想用第一張圖片中的線替換我圖表中的點,第二張圖片就是我所擁有的。

這是我的代碼

   plot(AM_leafP*1000,AM_cost_resorb[,1], type="p", pch= 15, col="red",ylab="Cost of reabsorbtion (kg C m^-2 yr^-1)", xlab="leaf P before senescence (g P/m2)",ylim=c(0,500))

我能夠像這樣通過我的點創建一條連續的線

   lines((AM_leafP*1000)[order(AM_leafP*1000)], (AM_cost_resorb[,1])[order(AM_leafP*1000)], pch=16)

但這不是我想要的,我想要一條沒有點的平滑線

我想我必須對 1/x 曲線使用預測,但我不確定如何,

第一張圖片第二張圖片

對於平滑,您可以將線性模型擬合為 foolws:

m <- lm(AM_cost_resorb~I(1/AM_leafP), data=data)

然后在覆蓋曝光變量范圍的新數據集上提取預測值。

newx <- seq(min(data$AM_leafP), max(data$AM_leafP), by=0.01)
pr <- predict(m, newdata=data.frame(AM_leafP=newx))

並可視化:

plot(AM_cost_resorb~AM_leafP, data=data, type="p", pch= 15, col="red",ylab="Cost of reabsorbtion (kg C m^-2 yr^-1)", xlab="leaf P before senescence (g P/m2)", ylim=c(0,500), las=1)
lines(newx, y=pr, col="blue", lwd=2)

在此處輸入圖像描述


數據:

data <- structure(list(AM_cost_resorb = c(176.01685819061, 21.6704613594849, 
19.007554742708, 50.1865574864131, 17.6174002411188, 40.2758022496774, 
11.0963214407251, 1249.94375253114, 694.894678288085, 339.786950220117, 
42.1452961176151, 220.352895161601, 19.6303352674776, 9.10350287678884, 
10.6222946396451, 44.1984352318898, 21.8069112975004, 42.1237630342764, 
22.7551891190248, 12.9587850506626, 12.0207189111152, 20.2704921282476, 
13.3441156357956, 9.13092569988769, 1781.08346869568, 71.2690023512206, 
80.2376892286713, 344.114362037227, 208.830841645638, 91.1778810401913, 
2220.0120768657, 41.4820962277111, 16.5730025748281, 32.30173229022, 
108.703930214512, 51.6770035143256, 709.071405759588, 87.9618878732223, 
10.4198968123037, 34.4951840238729, 57.8603720445067, 72.3289197551429, 
30.2366643066749, 23.8696161364716, 270.014690419247, 13.8170113452005, 
39.5159584479013, 27.764841260433, 18.0311836472615, 40.5709477295999, 
33.1888820958952, 9.03112843931787, 4.63738971549635, 12.7591169313099, 
4.7998894219979, 8.93458248803248, 7.33904760386628, 12.0940344070925, 
7.17364602165948, 6.514191844409, 9.69911157978057, 6.57874454980745, 
7.90556524435596), AM_leafP = c(0.02840637, 0.230728821, 0.2630533, 
0.099628272, 0.28381032, 0.12414402, 0.45059978, 0.00400018, 
0.00719533500000001, 0.014715103086687, 0.118637201789886, 0.022690875, 
0.254707825, 0.54923913, 0.470708088, 0.113126176837872, 0.22928510745, 
0.118697847481752, 0.219730100850697, 0.38583864, 0.4159485, 
0.24666396693114, 0.374696992776912, 0.547589605297248, 0.00280728, 
0.070156727820596, 0.062314855376136, 0.01453005323695, 0.02394282358199, 
0.0548378613646, 0.00225224, 0.120533928, 0.301695482, 0.15479046, 
0.045996497, 0.096754836, 0.00705147600000001, 0.0568428, 0.47985120103071, 
0.14494777, 0.08641493, 0.069128642, 0.165362156, 0.20947132, 
0.018517511, 0.36187275779699, 0.126531158458224, 0.180083867690804, 
0.277297380904852, 0.1232408972382, 0.15065285976048, 0.55364067, 
1.07819275643191, 0.39187665, 1.04169066418176, 0.55962324, 0.68128731, 
0.41342697, 0.69699564, 0.76755492, 0.515511133042674, 0.760023430328564, 
0.632465844687028)), class = "data.frame", row.names = c(NA, 
-63L))

假設 f(1/x) 很好地擬合數據。 可以使用lm()函數來確定所需函數 y= a/x + b,然后使用predict()函數來估計所需的點。
如果需要更復雜的非線性函數來擬合數據,則可能需要nls()

x<- c(176.01685819061, 21.6704613594849, 19.007554742708, 50.1865574864131, 17.6174002411188, 40.2758022496774, 11.0963214407251, 1249.94375253114, 694.894678288085, 339.786950220117, 42.1452961176151, 220.352895161601, 19.6303352674776, 9.10350287678884, 10.6222946396451, 44.1984352318898, 21.8069112975004, 42.1237630342764, 22.7551891190248, 12.9587850506626, 12.0207189111152, 20.2704921282476, 13.3441156357956, 9.13092569988769, 1781.08346869568, 71.2690023512206, 80.2376892286713, 344.114362037227, 208.830841645638, 91.1778810401913, 2220.0120768657, 41.4820962277111, 16.5730025748281, 32.30173229022, 108.703930214512, 51.6770035143256, 709.071405759588, 87.9618878732223, 10.4198968123037, 34.4951840238729, 57.8603720445067, 72.3289197551429, 30.2366643066749, 23.8696161364716, 270.014690419247, 13.8170113452005, 39.5159584479013, 27.764841260433, 18.0311836472615, 40.5709477295999, 33.1888820958952, 9.03112843931787, 4.63738971549635, 12.7591169313099, 4.7998894219979, 8.93458248803248, 7.33904760386628, 12.0940344070925, 7.17364602165948, 6.514191844409, 9.69911157978057, 6.57874454980745, 7.90556524435596)
y<- c(0.02840637, 0.230728821, 0.2630533, 0.099628272, 0.28381032, 0.12414402, 0.45059978, 0.00400018, 0.00719533500000001, 0.014715103086687, 0.118637201789886, 0.022690875, 0.254707825, 0.54923913, 0.470708088, 0.113126176837872, 0.22928510745, 0.118697847481752, 0.219730100850697, 0.38583864, 0.4159485, 0.24666396693114, 0.374696992776912, 0.547589605297248, 0.00280728, 0.070156727820596, 0.062314855376136, 0.01453005323695, 0.02394282358199, 0.0548378613646, 0.00225224, 0.120533928, 0.301695482, 0.15479046, 0.045996497, 0.096754836, 0.00705147600000001, 0.0568428, 0.47985120103071, 0.14494777, 0.08641493, 0.069128642, 0.165362156, 0.20947132, 0.018517511, 0.36187275779699, 0.126531158458224, 0.180083867690804, 0.277297380904852, 0.1232408972382, 0.15065285976048, 0.55364067, 1.07819275643191, 0.39187665, 1.04169066418176, 0.55962324, 0.68128731, 0.41342697, 0.69699564, 0.76755492, 0.515511133042674, 0.760023430328564, 0.632465844687028)


#data frame for prediction
df <- data.frame(x=sort(x))

# fit model y= a/x + b
model <-lm( y ~ I(1/x))
#summary(model)

#plot model
plot(df$x, predict(model, df), type="l", col="blue")
#optional
points(x, y)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM