[R] arrow on contour line

Rolf Turner ro||turner @end|ng |rom po@teo@net
Thu Jan 11 09:18:24 CET 2024


On Wed, 10 Jan 2024 19:13:19 -0500
Deepankar Basu <basu.15 using gmail.com> wrote:

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

My solution would be to use base graphics and avoid the obfuscation
induced by ggplot().  The graphics produced by ggplot() are devilish
pretty, but ggplot() is devilish difficult to work with.  IMHO you need
an IQ of at least 200 (which leaves me about 150 points short!) to be
able to cope with its intricate and unintuitive syntax.

I believe that what you want to do is triv in base graphics, but I must
confess that I have not gone through the details.

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:
         +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619



More information about the R-help mailing list