1

I am trying to fit a 3rd order polynomial on ggplot2 using stat_smoth but would like to force the regression line through a know x-intercept (35 for the following reproducible example).

Example:

x<-c(1,2,3,5,10,12,15,20,22,25,30,33,37)
y<-c(1000,800,100,10,1,0.3,0.25,0.2,0.1,0.1,0.03,0.05,0.03)
myformula<-y ~ poly(x,3)
ggplot(df, aes(x,y)) + stat_smooth(method = lm, formula = myformula) + geom_point() + 
stat_smooth(method = lm, formula = myformula) +
stat_poly_eq(formula = myformula, eq.with.lhs = "italic(psi)~`=`~",
                                  eq.x.rhs = "~italic(theta)", 
                                  aes(label = paste(..eq.label.., ..rr.label.., 
                                  sep = "~~~~")), label.x=0.15, parse = TRUE)+ 
xlim(0, 40)+  ylim(0, 2000)+ 
scale_y_log10(breaks = c(0, 0.1,10,1000), labels= c(0,0.1, 10,1000))

enter image description here

How can a get the best-fit regression line and equation when the x-intercept is 35? Also, how can I make the line touch the x-axis?

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
paml
  • 91
  • 2
  • 1
    You say the x intercept is at x = 35, but you have a logarithmic y scale, so y = 0 would be infinitely far down, off the page. How would you intend to show this on a plot? – Allan Cameron Nov 01 '20 at 22:07
  • That is a good point, I did not think about it. Would it be possible to make x=35 when y is a very small number (e.g. 0.001)? – paml Nov 02 '20 at 02:53

0 Answers0