Search code examples
rgradientcontourarrows

R: Plot arrows perpendicular to contour lines


I have two vectors representing x and y-coordinates in a scatter plot, and a thrid variable (z) for each (x,y)-coordinate representing the variable from which to draw contour lines. Example data are given as follows:

df<-data.frame(x=runif(n=30,min=-6,max=6),
               y=runif(n=30,min=-6,max=10),
               z=seq(1,100,length.out=30))

I use the R-package akima to generate the z-matrix for the contour plot

library(akima)
M1 <- interp(x=df$x,y=df$y,z=df$z)
contour(x=M1$x,y=M1$y,z=M1$z)

I now want to draw arrows perpendicular to the contourlines, preferably using something like the function "quiver" in the R-package pracma, with the origin of an arrow at every (x,y)-coordinate and with the arrow pointing in the direction of the gradient of the contourlines. Is there a way to do this?

My best idea so far is to somehow extract (x,y)-gradients of the contourlines and use these as velocities in the quiver function.

Grateful for any assistance.


Solution

  • The pracma package has a gradient function that can do this for you using the original M1$z values. For example, using your code to get M1 after set.seed(123):

    contour(x=M1$x,y=M1$y,z=M1$z, asp = 1) # asp = 1 needed so things look perpendicular
    
    library(pracma)
    g <- gradient(M1$z, M1$x, M1$y)
    
    x <- outer(M1$x, M1$y, function(x, y) x)
    y <- outer(M1$x, M1$y, function(x, y) y)
    
    quiver(x, y, g$Y, g$X, scale = 0.02, col = "blue")
    

    Note that the gradient labels in the quiver plot have been swapped. Maybe I set up the x and y values transposed from the way the package expects. Here's what you get:

    enter image description here