Feedback

Question:

In a

scatter plot

, I possess a pair of vectors that represent x and y-coordinates, along with a third variable (z) which corresponds to each (x,y)-coordinate and denotes the variable used for contour line generation. To illustrate, an instance of the data is provided below.

```
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))
```

To produce the z-matrix for the contour plot, I utilize the akima R-package.

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

Is there a way to draw arrows perpendicular to contourlines, using a function similar to “quiver” from the R-package pracma? The arrows should have their origin at each (x,y)-coordinate and point in the direction of the gradient of the contourlines.

My most promising approach is to extract gradients in the (x,y) direction from the contour lines and apply them as velocities in the quiver function.

Grateful for any assistance.

Solution:

The

pracma

package offers a

gradient

function that utilizes the initial

M1$z

values to obtain

M1

following

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")
```

It should be noted that the gradient labels have been interchanged in the

quiver

plot. Perhaps, I might have mistakenly arranged the

x

and

y

values in a transposed manner, which is different from what the package anticipates. As a result, the output looks like this: