如何在R中绘制衰减曲线?

时间:2018-04-21 13:08:30

标签: r

我尝试使用网络上的代码来绘制衰减曲线,但我从来没有得到带有点的图形,同时还有衰减曲线。我所得到的是一个散点图,其中点由线连接。任何帮助都是极好的。谢谢。

我确实使用了 ggplot2:geom_point和geom_smooth,geom_line和其他选项

df <- read.table(text = 
    "d weighted_LD
     1.400   0.00002198
     1.450   0.00001849
     1.500   0.00001652
     1.550   0.00001673
     1.600   0.00001067
     1.650   0.00001647
     1.700   0.00001861
     1.750   0.00001903
     1.800   0.00001949
     1.850   0.00001966
     1.900   0.00001781
     1.950   0.00001327
     2.000   0.00000964
     2.050   0.00001383
     2.100   0.00000492
     2.150   0.00001325
     2.200   0.00001706
     2.250   0.00002061
     2.300   0.00002108
     2.350   0.00001343
     2.400   0.00001627
     2.450   0.00001545
     2.500   0.00000920", header = TRUE)

1 个答案:

答案 0 :(得分:1)

不清楚你的衰变曲线是什么意思。

如果这是关于拟合y = beta0 * exp( beta1 * x)形式的一般指数模型,一个好的起点是log - 线性化你的数据并拟合线性模型:

fit <- lm(log(weighted_LD) ~ d, data = df);
fit;
#Call:
#lm(formula = log(weighted_LD) ~ d, data = df)
#
#Coefficients:
#(Intercept)            d
#   -10.5438      -0.2864

df$pred.y <- exp(predict(fit));

ggplot(df, aes(d, weighted_LD)) +
    geom_line() +
    geom_line(aes(d, pred.y), colour = "red", size = 1) +
    labs(x = "d (cM)", y = "Weighted LD")

enter image description here

stat_functionfit参数一起使用可生成相同的图:

ggplot(df, aes(d, weighted_LD)) +
    geom_line() +
    stat_function(fun = function(x) exp(coef(fit)[1] + coef(fit)[2] * x), colour = "red") + 
    labs(x = "d (cM)", y = "Weighted LD")

样本数据

df <- read.table(text = 
    "d weighted_LD
     1.400   0.00002198
     1.450   0.00001849
     1.500   0.00001652
     1.550   0.00001673
     1.600   0.00001067
     1.650   0.00001647
     1.700   0.00001861
     1.750   0.00001903
     1.800   0.00001949
     1.850   0.00001966
     1.900   0.00001781
     1.950   0.00001327
     2.000   0.00000964
     2.050   0.00001383
     2.100   0.00000492
     2.150   0.00001325
     2.200   0.00001706
     2.250   0.00002061
     2.300   0.00002108
     2.350   0.00001343
     2.400   0.00001627
     2.450   0.00001545
     2.500   0.00000920", header = T)