这是我之前发布的一个修订后的问题,为了清晰起见,已经进行了改进。
我尝试制作不同温度(t)
下CO2速率数据(y)
的散点图,其中包含两个分类变量depth
(矿物、有机)和substrate
(钙质、变粒岩、橄榄岩),拟合线符合方程y = a*exp(b*t)
,其中y = CO2
比率,a = basal respiration
(截距)、b = slope
和t = temperature
我已经拟合了所有的指数曲线,所以我有y
,a
,每个数据点的b
和t
。我正在努力弄清楚如何使用function exp_funct <- function(y,a,b,t){y=a*exp(b*t)}
为两组的每个类别绘制指数曲线,到目前为止,我已经使用GGplot2通过深度着色和通过基板刻面来产生基础散点图,但是我不知道拟合曲线的最佳方法是什么,我尝试使用geom_line
和stat_function
,但没有成功。
下面是我对一些可复制代码的尝试:
co2_data <- structure(list(chamber_temp = c(10, 10, 10, 10, 10, 15, 15, 15,
15, 15, 15, 19, 19, 19, 25, 25, 25, 25, 25, 25, 35, 35, 35, 35,
35, 35, 5, 5, 5, 5, 5, 5), substrate = c("Calcareous", "Metapelite",
"Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous",
"Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous",
"Calcareous", "Metapelite", "Calcareous", "Calcareous", "Metapelite",
"Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous",
"Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous",
"Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite"
), depth = c("Mineral", "Mineral", "Organic", "Mineral", "Organic",
"Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic",
"Mineral", "Organic", "Organic", "Mineral", "Organic", "Mineral",
"Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral",
"Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral",
"Organic", "Mineral", "Organic"), N.x = c(3, 6, 4, 5, 8, 6, 8,
7, 8, 8, 8, 3, 8, 4, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8, 6, 8,
8, 8, 8, 8), basal_respiration = c(0.0092, 0.0124666666666667,
0.04935, 0.0101, 0.05785, 0.01315, 0.01415, 0.013, 0.0402, 0.01075,
0.05785, 0.0171, 0.01415, 0.03105, 0.01315, 0.01415, 0.013075,
0.0402, 0.01075, 0.05785, 0.01315, 0.01415, 0.013075, 0.0402,
0.01075, 0.05785, 0.01315, 0.01415, 0.013075, 0.0402, 0.01075,
0.05785), sd.x = c(0.00744781847254617, 0.00234065517893317,
0.00178978583448784, 0.00166132477258362, 0.0118691677407113,
0.00666175652512158, 0.00727284577825528, 0.00256059888828115,
0.00986798575481049, 0.00193833507349551, 0.0118691677407113,
0.00294448637286709, 0.00727284577825528, 0.000866025403784439,
0.00666175652512158, 0.00727284577825528, 0.00238012604708238,
0.00986798575481049, 0.00193833507349551, 0.0118691677407113,
0.00666175652512158, 0.00727284577825528, 0.00238012604708238,
0.00986798575481049, 0.00193833507349551, 0.0118691677407113,
0.00666175652512158, 0.00727284577825528, 0.00238012604708238,
0.00986798575481049, 0.00193833507349551, 0.0118691677407113),
se.x = c(0.0043, 0.000955568475364854, 0.000894892917243919,
0.000742967024840269, 0.0041963844982488, 0.00271965071286737,
0.00257133928416413, 0.000967815409397198, 0.00348885982193938,
0.000685304937340201, 0.0041963844982488, 0.0017, 0.00257133928416413,
0.00043301270189222, 0.00271965071286737, 0.00257133928416413,
0.000841501633985341, 0.00348885982193938, 0.000685304937340201,
0.0041963844982488, 0.00271965071286737, 0.00257133928416413,
0.000841501633985341, 0.00348885982193938, 0.000685304937340201,
0.0041963844982488, 0.00271965071286737, 0.00257133928416413,
0.000841501633985341, 0.00348885982193938, 0.000685304937340201,
0.0041963844982488), ci.x = c(0.0185014067379227, 0.00245636696547958,
0.00284794865810747, 0.00206280715944113, 0.00992287255356713,
0.00699108472177222, 0.00608025123040774, 0.00236815899497472,
0.00824984254536553, 0.00162048867457091, 0.00992287255356713,
0.00731450964057409, 0.00608025123040774, 0.00137803967327781,
0.00699108472177222, 0.00608025123040774, 0.00198983517147669,
0.00824984254536553, 0.00162048867457091, 0.00992287255356713,
0.00699108472177222, 0.00608025123040774, 0.00198983517147669,
0.00824984254536553, 0.00162048867457091, 0.00992287255356713,
0.00699108472177222, 0.00608025123040774, 0.00198983517147669,
0.00824984254536553, 0.00162048867457091, 0.00992287255356713
), N.y = c(3, 6, 4, 5, 8, 6, 8, 7, 8, 8, 8, 3, 8, 4, 6, 8,
8, 8, 8, 8, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8), slope = c(0.120293333333333,
0.0593333333333333, 0.07685, 0.05602, 0.067475, 0.108913333333333,
0.15655, 0.0600714285714286, 0.08535, 0.057525, 0.067475,
0.0975333333333333, 0.15655, 0.09385, 0.108913333333333,
0.15655, 0.058125, 0.08535, 0.057525, 0.067475, 0.108913333333333,
0.15655, 0.058125, 0.08535, 0.057525, 0.067475, 0.108913333333333,
0.15655, 0.058125, 0.08535, 0.057525, 0.067475), sd.y = c(0.0326433842199814,
0.00744813175680094, 0.00456106712659804, 0.00374259268422306,
0.00379877799900366, 0.0244087448810189, 0.0131734581640509,
0.00707406396298344, 0.00967780966954816, 0.00357481268240529,
0.00379877799900366, 0.00594670777265315, 0.0131734581640509,
0.00225166604983954, 0.0244087448810189, 0.0131734581640509,
0.0085558250833653, 0.00967780966954816, 0.00357481268240529,
0.00379877799900366, 0.0244087448810189, 0.0131734581640509,
0.0085558250833653, 0.00967780966954816, 0.00357481268240529,
0.00379877799900366, 0.0244087448810189, 0.0131734581640509,
0.0085558250833653, 0.00967780966954816, 0.00357481268240529,
0.00379877799900366), se.y = c(0.0188466666666667, 0.00304068705686359,
0.00228053356329902, 0.00167373833080324, 0.00134307084165888,
0.00996482837004454, 0.00465752079973885, 0.0026737448578026,
0.00342162242218512, 0.00126388714460023, 0.00134307084165888,
0.00343333333333334, 0.00465752079973885, 0.00112583302491977,
0.00996482837004454, 0.00465752079973885, 0.00302494096754678,
0.00342162242218512, 0.00126388714460023, 0.00134307084165888,
0.00996482837004454, 0.00465752079973885, 0.00302494096754678,
0.00342162242218512, 0.00126388714460023, 0.00134307084165888,
0.00996482837004454, 0.00465752079973885, 0.00302494096754678,
0.00342162242218512, 0.00126388714460023, 0.00134307084165888
), ci.y = c(0.0810906617800115, 0.007816334916228, 0.00725767561259645,
0.00464704259594057, 0.00317585788379371, 0.0256154068032699,
0.0110132866353603, 0.0065424179794951, 0.00809085135929258,
0.00298861819339805, 0.00317585788379371, 0.0147724410388065,
0.0110132866353603, 0.0035829031505223, 0.0256154068032699,
0.0110132866353603, 0.00715284877149766, 0.00809085135929258,
0.00298861819339805, 0.00317585788379371, 0.0256154068032699,
0.0110132866353603, 0.00715284877149766, 0.00809085135929258,
0.00298861819339805, 0.00317585788379371, 0.0256154068032699,
0.0110132866353603, 0.00715284877149766, 0.00809085135929258,
0.00298861819339805, 0.00317585788379371), N = c(3, 6, 4,
5, 8, 6, 8, 7, 8, 8, 8, 3, 8, 4, 6, 8, 8, 8, 8, 8, 6, 8,
8, 8, 8, 8, 6, 8, 8, 8, 8, 8), co2_rate_u_m_h_g = c(0.0303333333333333,
0.0113333333333333, 0.0645, 0.0066, 0.129375, 0.0615, 0.1325,
0.0254285714285714, 0.132, 0.021, 0.14325, 0.085, 0.208,
0.17, 0.198666666666667, 0.71025, 0.0575, 0.344, 0.05225,
0.3115, 0.5155, 3.27125, 0.10375, 0.7835, 0.079, 0.6065,
-0.00766666666666667, 0.024625, 0.02675, 0.065125, 0.012125,
0.061), sd = c(0.0161658075373095, 0.0109483636524673, 0.0137719521734091,
0.00634822809924155, 0.0181102772716804, 0.0332009036021612,
0.0291498591028205, 0.00639940473422184, 0.0225895045161622,
0.0101136400116731, 0.0263425240140077, 0.00435889894354067,
0.0731358813637816, 0.0127279220613579, 0.0471197057149837,
0.302157834819581, 0.0214941852602047, 0.0326233921333407,
0.0116833214455479, 0.0554204706893452, 0.130053450550149,
1.288200932641, 0.0353138985184506, 0.129266060068814, 0.0200997512422418,
0.0639262968470052, 0.0164032517101539, 0.0136793640203045,
0.00948306761699881, 0.0180193348537461, 0.00918753036924038,
0.0156296421675518), se = c(0.00933333333333333, 0.00446965074449646,
0.00688597608670453, 0.00283901391331568, 0.0064029499339869,
0.0135542121374378, 0.0103060315211184, 0.00241874763794291,
0.00798659591351123, 0.00357571171736681, 0.00931348868193715,
0.00251661147842358, 0.0258574388301924, 0.00636396103067893,
0.0192365393053024, 0.106828926994785, 0.00759934207678533,
0.0115341109013965, 0.00413067791046458, 0.0195940953204931,
0.0530940988560248, 0.455447807500643, 0.012485348556265,
0.0457024538259631, 0.00710633520177595, 0.0226013589983308,
0.00669659946871877, 0.00483638553053828, 0.0033527707092152,
0.00637079693377748, 0.00324828251322361, 0.00552591298209755
), ci = c(0.0401580921443283, 0.0114896030154409, 0.0219142491554048,
0.00788236628321375, 0.0151405706956398, 0.0348422115168589,
0.0243698920725162, 0.00591846226021141, 0.0188852983847605,
0.00845521464359003, 0.0220229012042435, 0.0108281052473581,
0.0611431269419499, 0.0202529642690536, 0.0494490985187144,
0.252610271543504, 0.0179695885709161, 0.0272738383580029,
0.00976750116260314, 0.0463326729828588, 0.136482726098776,
1.07696293095078, 0.0295231579857332, 0.108069130674172,
0.0168038125580669, 0.0534437216064078, 0.0172141569548202,
0.0114362345155632, 0.00792804292904021, 0.0150645409315832,
0.0076809676067933, 0.0130667078496593)), row.names = c(NA,
-32L), class = "data.frame")
exp_funct <- function(y,a,b,t){y=a*exp(b*t)}
ggplot(co2_data ,
aes(x = chamber_temp, y = co2_rate_u_m_h_g, colour = substrate, linetype = substrate,
shape = substrate, fill = substrate),
ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se) +
geom_point(stat="identity", position = "dodge", width = 0.7) +
geom_errorbar(aes(ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se),
width=0.1, size=0.1, color="black") +
facet_wrap(~depth) +
stat_function(data = co2_data ,
aes(y= co2_rate_u_m_h_g, a = basal_respiration, b = slope, c = chamber_temp),
fun = exp_funct(y =co2_rate_u_m_h_g, a = basal_respiration, b = slope, t = chamber_temp))
exp_funct(y = co2_rate_u_m_h_g,a =基础呼吸,b =斜率,:未找到对象"basal_respiration"此外:警告信息:忽略未知参数:宽度
2条答案
按热度按时间wqsoz72f1#
除了Allan的答案之外,一位同事还想出了如何使用nls来完成此任务
j9per5c42#
你不能用这种方式给
stat_function
提供参数,在这种情况下,我可能只会在geom_line
调用中生成一个小的摘要 Dataframe :