直接通过 R 中的某些数据点绘制曲线 (ggplot)

Plotting a curved line directly through some datapoints in R (ggplot)

提问人:Anne 提问时间:1/15/2021 更新时间:1/15/2021 访问量:632

问:

我正在努力在模拟器的运动剖面上绘制图形。 我想展示的是模拟器随时间推移的位移。

一些示例数据:

Time = c(0, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44)
Displacement = c(0, 0, 7, 0, 0, 7, 0, 0, -7, 0, 0)
DD = as.data.frame(Time, Displacement)

我想绘制一条直接穿过这些数据点的曲线/平滑线。

在此处输入图像描述偏离路线使用geom_line会产生一条尖线。

我最接近获得更平滑的线条是使用这段代码:

ggplot(DD, aes(x=Time, y=Displacement, c(0,7))) + 
  geom_smooth(method = "loess", se = FALSE, span = 0.2, colour="black")

在此处输入图像描述但是,曲线仍然很尖锐,我希望得到一个更漂亮的情节。

希望有人能:)提供帮助 安妮

R ggplot2 曲线 数据点

评论


答:

0赞 Duck 1/15/2021 #1

尝试多项式拟合:

library(ggplot2)
#Code
ggplot(DD, aes(x=Time, y=Displacement, c(0,7))) + 
  geom_smooth(method = "lm",formula = y~poly(x,3), se = FALSE, span = 0.2, colour="black")

输出:

enter image description here