简体   繁体   中英

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

I am struggling with plotting a figure on the motion profile of a simulator. What I'm trying to show are the displacements of the simulator over time.

Some sample data:

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)

I want to plot a curved/smoothed line that goes directly through these datapoints.

enter image description here Using geom_line off course generates a spiky line.

The closest I have gotten to get a smoother line is using this piece of code:

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

enter image description here However, the curves are still quite spiky and I am hoping to get a more beautiful plot.

Hoping anyone can be of help:) Anne

Try with a polynomial fit:

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")

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM