Search code examples
rlinear-regressionboxplot

Scatterplot - adding equation and r square value


I am newbie at R. Now I want to plot data (two variables) and showing regression line including the boxplot. I am able to show those data except the r square value and equation chart.

Below is my script in showing the graph

library (car)
scatterplot(FIRST_S2A_NDVI, MEAN_DRONE_NDVI, 
            main = "NDVI Value from Sentinel and Drone", 
            xlab = "NDVI Value from Sentinel", 
            ylab = "NDVI Value from Drone", 
            pch = 15, col = "black", 
            regLine = list(col="green"), smooth = FALSE)

The figure is like this. Comparison NDVI Value

Now, the final touch is to add the equation and r square value on my figure. What script do I need to write. I tried this script from Add regression line equation and R^2 on graph but still no idea how to show them.

Thanks for read and hopefully helping me in this.

p.s.

Content of my data

OBJECTID SAMPLE_GRID FIRST_S2A_NDVI MEAN_DRONE_NDVI
1         1           1      0.6411405       0.8676092
2         2           2      0.4335293       0.5697814
3         3           3      0.7350439       0.7321858
4         4           4      0.7268013       0.8271566
5         5           5      0.3638939       0.5682631
6         6           6      0.1953890       0.3168246
7         7           7      0.4841993       0.7380627
8         8           8      0.4137447       0.3239288
9         9           9      0.8219178       0.8676065
10       10          10      0.2647872       0.2296441
11       11          11      0.8126657       0.8519964
12       12          12      0.2648504       0.2465738
13       13          13      0.5992035       0.8016030
14       14          14      0.2420299       0.3933670
15       15          15      0.5059137       0.7593807
16       16          16      0.7713419       0.8026068
17       17          17      0.3762540       0.5941540
18       18          18      0.5876435       0.7763927
19       19          19      0.2491609       0.5095306
20       20          20      0.3213648       0.4456958
21       21          21      0.2101466       0.1960858
22       22          22      0.3749034       0.4956361
23       23          23      0.5712630       0.7350484
24       24          24      0.8444895       0.8577550
25       25          25      0.3331450       0.4390229
26       26          26      0.1851611       0.4573663
27       27          27      0.4914998       0.2750837
28       28          28      0.7121390       0.7780228

Solution

  • For adding the equation and the R squared value to your current plot. You can simply create a model with the y and x variables and format a equation and paste in over the plot using mtext function.

    m <- lm(MEAN_DRONE_NDVI~FIRST_S2A_NDVI)
    
    eq <- paste0("y = ",round(coef(m)[2],3),"x ",
                 ifelse(coef(m)[1]<0,round(coef(m)[1],3),
                        paste("+",round(coef(m)[1],3))))         
    
    mtext(eq, 3,-1)
    mtext(paste0("R^2 = ",round(as.numeric(summary(m)[8]),3)), 3, -3)
    

    You can change the variables in your model and also change the position of the text with the 2nd and 3rd arguments in the mtext function