我如何使我的线条更贴合整洁?

问题描述 投票:1回答:2

我正在尝试使用以下代码进行简单绘制。

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函数可以接受参数来更改线宽或线型吗?有没有一种方法可以使拟合更好,以使曲线看起来像下面的曲线一样好的连续函数?

r ggplot2 curve-fitting
2个回答
1
投票

您可以更改拟合的span使其或多或少平滑。您也可以使用size更改线宽,并使用linetype键入:

ggplot(p) + 
  geom_point(aes(x = eta, y = R), size = 3) +
  geom_smooth(
    aes(y=R, x=eta),
    method = "loess",
    se = FALSE, 
    span = 0.9, 
    linetype = "dashed",
    size = 0.5)

1
投票

您可以在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

© www.soinside.com 2019 - 2024. All rights reserved.