1

I am developing a shiny app in which, I am generating scatterplots by uploading the data files in .txt format. I doing a polynomial fit on the scatterplot. I want the plot to show R^2 value. Here is my attempt:

#plot
 g <- ggplot(data =  df, aes_string(x = df$x, y = df$y)) + theme_bw() +
      geom_point(colour = "blue", size = 0.1)+
      geom_smooth(formula = y ~ poly(x,input$degree, raw = TRUE), method = "lm", color = "green3", level = 1, size = 0.5)+
      stat_poly_eq(formula = y ~ poly(x,input$degree, raw = TRUE),aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE) 
        
 ggplotly(g)

A slider is used to vary the degree of the polynomial function, its handle is input$degree

I used stat_poly_eq from the ggpmisc package to get the value of the R^2

But when I run this code, the R^2 value does not get reflected on the ggplot as a legend. Which, according to the examples that I have seen, should get reflected on the plot as the legend of the plot.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
kolas0202
  • 163
  • 1
  • 7
  • 2
    Hey, kolas0202! Check this out, seems like it's going to help you - https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph – rg4s Aug 17 '20 at 18:07
  • 3
    You can't use `ggpmisc::stat_poly_eq` in `plotly`, it's not implemented. You could manually add the formula as shown in the answer linked by k1rgas. – Ian Campbell Aug 17 '20 at 18:14
  • Yes, I have already read the link that you have shared. In fact, the code you see above in my question is inspired by that post. It is working in R, but when I try that same thing in my shiny app, it is not working. – kolas0202 Aug 17 '20 at 18:23
  • Ian Campbell, Thank you for the clarification, I will try that. – kolas0202 Aug 17 '20 at 18:24

0 Answers0