rcoordinateslinespoints

How do I estimate the coordinates of points along a graphed line in R?


Assume I have data:

x <- c(1900,1930,1944,1950,1970,1980,1983,1984)
y <- c(100,300,500,1500,2500,3500,4330,6703)

I then plot this data and add a line graph between my known x and y coordinates:

plot(x,y)
lines(x,y)

Is there a way to predict coordinates of unknown points along the graphed line?


Solution

  • You can use approxfun.

    f <- approxfun(x, y=y)
    
    f(seq(1900, 2000, length.out = 10))
    # [1]  100.0000  174.0741  248.1481  347.6190  574.0741 1777.7778 2333.3333
    # [8] 3277.7778        NA        NA
    

    Note the NA, when the sequence is outside the range of interpolated points (there are left and right options to approxfun).