R,ggplot2:将曲线拟合到散点图 [英] R, ggplot2: Fit curve to scatter plot

查看:1231
本文介绍了R,ggplot2:将曲线拟合到散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot2将曲线拟合到以下散点图。

I am trying to fit curves to the following scatter plot with ggplot2.

我找到了geom_smooth函数,但是尝试使用不同的方法和跨度,我似乎从未得到过正确的曲线...

I found the geom_smooth function, but trying different methods and spans, I never seem to get the curves right...

这是我的散点图:

This is my scatter plot:

这是我的最佳尝试:

And this is my best attempt:

任何人都可以获得更好的曲线,该曲线正确地拟合并且不会看起来不那么摇摆吗?谢谢!

Can anyone get better curves that fit correctly and don't look so wiggly? Thanks!

在下面查找MWE:

my.df <- data.frame(sample=paste("samp",1:60,sep=""),
                    reads=c(523, 536, 1046, 1071, 2092, 2142, 4184, 4283, 8367, 8566, 16734, 17132, 33467, 34264, 66934, 68528, 133867, 137056, 267733, 274112, 409, 439, 818, 877, 1635, 1754, 3269, 3508, 6538, 7015, 13075, 14030, 26149, 28060, 52297, 56120, 104594, 112240, 209188, 224479, 374, 463, 748, 925, 1496, 1850, 2991, 3699, 5982, 7397, 11963, 14794, 23925, 29587, 47850, 59174, 95699, 118347, 191397, 236694),
                    number=c(17, 14, 51, 45, 136, 130, 326, 333, 742, 738, 1637, 1654, 3472, 3619, 7035, 7444, 13133, 13713, 21167, 21535, 11, 22, 30, 44, 108, 137, 292, 349, 739, 853, 1605, 1832, 3099, 3565, 5287, 5910, 7832, 8583, 10429, 11240, 21, 43, 82, 124, 208, 296, 421, 568, 753, 908, 1127, 1281, 1448, 1608, 1723, 1854, 1964, 2064, 2156, 2259),
                    condition=rep(paste("cond",1:3,sep=""), each=20))

png(filename="TEST1.png", height=800, width=1000)
print(#or ggsave()
ggplot(data=my.df, aes(x=reads, y=log2(number+1), group=condition, color=condition)) +
    geom_point()
)
dev.off()

png(filename="TEST2.png", height=800, width=1000)
print(#or ggsave()
ggplot(data=my.df, aes(x=reads, y=log2(number+1), group=condition, color=condition)) +
    geom_point() +
    geom_smooth(se=FALSE, method="loess", span=0.5)
)
dev.off()


推荐答案

这是一个非常广泛的问题,因为您实际上是在寻找方差少(偏差大)的模型,其中有很多。这是一个:

This is a very broad question, as you're effectively looking for a model with less variance (more bias), of which there are many. Here's one:

ggplot(data = my.df, 
       aes(x = reads, y = log2(number + 1), color = condition)) +
    geom_point() +
    geom_smooth(se = FALSE, method = "gam", formula = y ~ s(log(x)))

有关文档,请参见?mgcv :: gam 或有关建模的适当文字。根据您的用例,使模型超出ggplot可能更有意义。

For documentation, see ?mgcv::gam or a suitable text on modeling. Depending on your use case, it may make more sense to make your model outside of ggplot.

这篇关于R,ggplot2:将曲线拟合到散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆