gpt4 book ai didi

r - 如何让我的线条更合身、更整洁?

转载 作者:行者123 更新时间:2023-12-03 20:25:52 25 4
gpt4 key购买 nike

我正在尝试使用以下代码制作一个简单的图。

eta = c(0, 0.2, 0.4, 0.6, 0.8, 1)
R = c(0, 0.647058807, 0.864035125, 0.992063541, 0.996376783, 1)

p = as.data.frame(cbind(eta, R))

library(ggplot2)
ggplot(p) +
geom_point(aes(x = eta, y = R), size = 3) +
geom_smooth(aes(y=R, x=eta), method = "loess", se = FALSE)

我得到如下图:



geom_smooth 函数可以接受参数来更改线宽或线型吗?有没有办法获得更好的拟合,使曲线看起来像一个很好的连续函数,如下所示?

最佳答案

您可以在 stat_smooth 中设置自定义公式.按照您的示例,如果您想拟合四阶多项式,您可以使用

ggplot(p, aes(x = eta, y = R)) + 
geom_point(size = 3) +
stat_smooth(method = 'lm', formula = y~ poly(x, 4), se = FALSE)
enter image description here
编辑:添加一个在 1.0 处具有渐近线的方程。这有点棘手,因为它需要使用非线性方法求解。
ggplot(p, aes(x = eta, y = R)) + 
geom_point(size = 3) +
stat_smooth(method = 'nls',
formula = y ~ 1-exp(-k*x),
se = FALSE)
enter image description here
虽然绘图方便, stat_smooth使得访问拟合模型变得棘手。您可以安装在 ggplot 之外使用 nls直接使用函数,然后使用 predict 进行预测.
# Fit model
fit_nls <-nls(R ~ 1-exp(-k*eta), data=p)

# Predict model
df_pred <- data.frame(eta = seq(0,1.1,.01)) %>%
mutate(R_pred = predict(fit_nls, newdata = .))

# Plot it
ggplot(p) +
geom_point(aes(x = eta, y = R), size = 3) +
geom_line(data = df_pred, aes(x = eta, y = R_pred))

关于r - 如何让我的线条更合身、更整洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61280833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com