简体   繁体   中英

Smoothen curve and get its equation in R

I have this code in R :

 plot(p,vec, pch = 4, xlab= "Values of x",ylab= "f(x)" )
  lines(p,vec)
  return (vec)

And I have this plot :

获得的情节

I would like to smoothen the curve and get its equation in R, could you help me please?

I cannot unfortunately reproduce your example but I would guess that this could be a good solution:

library(ggplot2)
data_to_plot <- data.frame(p, vec)
p <- ggplot(data_to_plot, aes(x=p, y=vec)) + geom_point(pch=4) + geom_smooth(colour='black')

# Only plot
print(p)

# Dataset using for plotting
ggplot_build(p)

# Loess model as used in plot
loessMod <- loess(vec ~ p, data=data_to_plot)

Check the ggplot cheat sheet for more information on how you can make it a nice plot: https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf

For more info on using the smooth curve, see: http://r-statistics.co/Loess-Regression-With-R.html

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