0

So I am graphing some data and getting a smoothed curve using geom_smooth. How can I display the equation that geom_smooth uses on the graph - I have found solutions for linear data but not for sinusoidal data - and perhaps also the r^2 value.

  ggplot(all_year_month_eng_wth,aes(x = year_month, y=energy_sum_mean,colour="Energy Cons.",group=1)) +
  geom_line()+
  geom_point()+
  geom_smooth(method="lm", formula=y~cos(2*pi*x/12) + sin(2*pi*x/12)) +
  theme(legend.position = c(0.8,0.9),axis.text.x = element_text(angle = 90,hjust = 1))
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Have you checked out this answer (https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph) or (https://stackoverflow.com/questions/37494969/ggplot2-add-regression-equations-and-r2-and-adjust-their-positions-on-plot) – Peter Apr 19 '20 at 18:20
  • @Peter Those solutions work for linear or polynomial lines but not for sinusoidal ones. I appreciate the help I am new to R and to Stack Overflow, please forgive me if I do something incorrectly. – Rohith Ramanujam Apr 19 '20 at 18:26
  • 1
    Could you add the `all_year_month_eng_wth` dataframe or a sample set from the dataframe to make your question reproducible? – Peter Apr 19 '20 at 18:31
  • https://drive.google.com/open?id=18f9gqQ7vPdCi-CuV1nmUu_7_JocPfjCf – Rohith Ramanujam Apr 19 '20 at 18:42
  • 1
    link should have it @Peter – Rohith Ramanujam Apr 19 '20 at 18:42
  • Have a look at this solution https://stackoverflow.com/a/51974753/4927395 – Mark Neal Apr 19 '20 at 19:10
  • I'm not sure I have time to spend on this. As well as the link @Mark Neal gave try: https://stats.stackexchange.com/questions/60994/fit-a-sinusoidal-term-to-data and https://stats.stackexchange.com/questions/60500/how-to-find-a-good-fit-for-semi-sinusoidal-model-in-r – Peter Apr 19 '20 at 19:57

0 Answers0