Search code examples
rscatter-plotraster

Adding a line to a scatter plot in R


I have a scatter plot of two raster images. What I need is to create a triangle around the scatter plot. So far what I have it

minveg <- quantile(veg, 0.01, na.rm=TRUE)
mintemp <- quantile(temps, 0.01, na.rm=TRUE)
plot(veg, temps, xlab='veg', ylab='temp')
abline(h=mintemp, v=minveg)

This give me the following image

enter image description here

What I need is a third line that goes along the scatter plot and completes the triangle with the other two lines. I'm sure I could add an equation just looking at it and fit my best idea but I have to do this for hundreds of images and was looking for a more automated way to do it. Any suggestions are helpful. Thanks!


Solution

  • You can use quantile regression for this.

     library(quantreg)
    
     # example data
     set.seed(0)
     x = rnorm(1000)
     y = rnorm(1000) - x
    
    
     minx <- quantile(x, 0.01, na.rm=TRUE)
     miny <- quantile(y, 0.01, na.rm=TRUE)
     plot(x,y, xlim=c(-6,6), ylim=c(-6,6))
     abline(h=minx, v=miny, col='red') 
     abline(rq(y ~ x, tau=.99), col='blue')