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.