0

I am trying to export the fitted equation, seen as a label in the graph of the example below, to a text file and I am not sure how to approach the problem. The equation is a string (eq.label) in stat I have attempted to output to the console using geom_debug(), which outputs the data points as a tibble, however it does not output the equation. Any suggestions?

enter image description here

library(ggplot2)
library(ggpmisc)
library(gginnards)
library(broom)

x<-rnorm(100,1, 2)
y<-rnorm(100,0.5, 2)

df<-data.frame(x,y)
str(df)
qplot(x,y)

 my.formula <- y ~ poly(x, 5, raw = TRUE)

         ggplot(df, aes(x, y) ) 
    + geom_point(colour = "blue") 
    + stat_smooth(method = "lm",formula = my.formula)
    + stat_poly_eq(formula = my.formula, eq.with.lhs = "italic(y)~`=`~",
   eq.x.rhs = "~italic(x)",aes(label = paste(..eq.label.., 
   ..rr.label.., sep = "~~~")), parse = TRUE, size = 4) 
    + labs(x="x", y="y")+ theme(legend.position="none", 
    axis.text=element_text(size=14), axis.title=element_text(size=14))
MacUser
  • 492
  • 9
  • 22
  • 1
    You have three options for `output.type=` in stat_poly_eq(), text, numeric, expression...[stat_poly_eq](https://www.rdocumentation.org/packages/ggpmisc/versions/0.3.9/topics/stat_poly_eq) – Chris Jul 21 '21 at 05:45
  • I didn't understand the `qplot` above, but note [prototype plot](https://stackoverflow.com/questions/16486819/how-to-deal-with-data-of-class-uneval-error-from-ggplot2) that doesn't get me any closer to where the evaluated label resides, could be extracted from, as it is not apparent `plot_holder <- ggplot(df...` puts it 'in one place'... – Chris Jul 21 '21 at 12:50
  • @Chris Thank you for the comments I have solved it using + geom_("debug") option in stat_plot_eq() as you have indicated in your first comment, if you delete the first comment and repost it as a reply I will choose it as a solution – MacUser Jul 22 '21 at 06:10
  • 1
    MacUser,I think you should post your solution and accept it, as my noodling around didn't extend to the utility of `+geom(debug)`, and sketching it out completely will help others going forward. Glad you solved it as was interesting to investigate. – Chris Jul 22 '21 at 14:39

0 Answers0