[R] arrow on contour line

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Thu Jan 11 09:56:03 CET 2024


В Wed, 10 Jan 2024 19:13:19 -0500
Deepankar Basu <basu.15 using gmail.com> пишет:

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

Can you use the information about the function (i.e. the myg object),
or are you restricted to the d2 data.frame?

Both ggplot and the core graphics use the algorithm from the
contourLines() function in order to draw the contour lines. (Lattice
does the same, but ports the function from C to R.) This function
returns a list of levels combined with vectors of `x` and `y`
coordinates for drawing the contour.

If you can use the myg function, you can differentiate it numerically
or symbolically, process the contourLines() output, evaluate the
gradient at one point per contour and draw additional arrows there.

If you are limited to the tabulated function values from the d2
data.frame, you have a more complicated problem on your hands. You can
either locate grid points closest to the contour segments at both sides
of the segment, determine the direction of growth, and draw an arrow
perpendicular to the contour segment (not equal to the gradient), or
find a way to estimate the gradient at arbitrary points between the
grid (e.g. by using a Taylor series stencil) and then go to the
previous paragraph.

-- 
Best regards,
Ivan



More information about the R-help mailing list