[R] arrow on contour line

Deepankar Basu b@@u@15 @end|ng |rom gm@||@com
Thu Jan 11 01:13:19 CET 2024


Hello,

I am drawing contour lines for a function of 2 variables at one level of
the value of the function and want to include a small arrow in any
direction of increase of the function. Is there some way to do that?

Below is an example that creates the contour lines. How do I add one small
arrow on each line in the direction of increase of the function (at some
central point of the contour line)? Any direction will do, but perhaps the
direction of the gradient will be the best.

Thanks in advance.
DB

--------------------------------------------

library(tidyverse)

x <- seq(1,2,length.out=100)
y <- seq(1,2,length.out=100)

myf <- function(x,y) {x*y}
myg <- function(x,y) {x^2 + y^2}

d1 <- expand.grid(X1 = x, X2 = y) %>%
  mutate(Z = myf(X1,X2)) %>%
  as.data.frame()

d2 <- expand.grid(X1 = x, X2 = y) %>%
  mutate(Z = myg(X1,X2)) %>%
  as.data.frame()

ggplot(data = d1, aes(x=X1,y=X2,z=Z))+
  stat_contour(breaks = c(2)) +
  stat_contour(data=d2, aes(x=X1,y=X2,z=Z), breaks=c(6))

	[[alternative HTML version deleted]]



More information about the R-help mailing list