1

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 :

Plot obtained

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

rawr
  • 20,481
  • 4
  • 44
  • 78
  • 4
    Hi Sarah, it would be easier to help if you provide the data you used to plot the curve (for example with `dput`). See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Ian Campbell Apr 02 '20 at 18:51

1 Answers1

0

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

Jan Janiszewski
  • 432
  • 3
  • 14