0

I have a scatter plot in R ggplot2 and I am using ggpmisc's stat_poly_line to draw a trendline and calculate R2, my problem is that due to my data, my trendline starts at a value different from 0 and I want to force the trendline to start at 0 similar to the excel option under trendline.

Any tips for ggpmisc or other pacakges?

my data

  Y       X
0.015   0.014
0.022   
0.094   0.093
0.220   0.322
0.490   0.237
0.970   0.199
0.180   0.119
0.130   0.179
0.230   0.233
0.220   0.091
0.059   0.125
0.065   0.049
0.250   0.145
0.140   0.080
0.210   0.074
0.860   0.592
0.280   
0.032   0.001
0.001   0.001
0.003   0.000
0.130   0.001
0.003   0.000
0.028   0.000
0.006   0.000
0.090   0.000
0.012   0.000
0.067   0.001
0.039   0.000
0.015   0.000
0.001   0.000
0.001   0.000
0.145   0.000

my plot

For reference this is what it looks like in Excel with a set intercept=0

Nomye
  • 3
  • 2
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jun 20 '23 at 13:59
  • It looks like you have several data points at x = 0, and their y value is clearly not 0. Why would you start the trendline at y = 0? – Allan Cameron Jun 20 '23 at 14:10
  • I included some sample data and what I want it to look like hope that makes it easier to answer – Nomye Jun 20 '23 at 14:25
  • What does your current R code for the plot look like? Assuming you are passing a formula you should be able add `+0` for an intercept free model – MrFlick Jun 20 '23 at 14:26
  • my current code is ggplot(df, aes(y=y, x=x)) + stat_poly_line(fullrange=TRUE) + stat_poly_eq(use_label(c('eq', 'R2'))) – Nomye Jun 20 '23 at 14:27
  • ggplot(df, aes(y=y, x=x)) + stat_poly_line(formula = y ~ x + 0, fullrange=TRUE) + stat_poly_eq(formula = y ~ x + 0, use_label(c('eq', 'n'))) R^2 is not intrepretable for a regression through the origin. – Pedro J. Aphalo Jun 20 '23 at 22:08

0 Answers0