[R] Query on contour plots

Abby Spurdle @purd|e@@ @end|ng |rom gm@||@com
Wed Jun 3 02:58:01 CEST 2020

> The contour lines are actually useful to see groupings.
> However w/o a legend for density it is not possible to see what is
> presented.

I need to re-iterate, that the diagonal lines, may be important.

Also, I'm not sure I see the point in adding density values.
Unless people have a good knowledge of probability theory and
calculus, I doubt that specific density values will be useful.
i.e. If I said the density was 0.0035, what does that tell you...?

If you really want to add a legend, it's possible.

But this creates at least two problems:
(1) In the base graphics system, the resulting plots can't be nested.
(2) It's difficult to interpret specific color-encoded values.

In my opinion, a better idea, is to label the contour lines.
In my packages, this is possible by using contour.labels=TRUE,
however, the defaults are ugly.
(Something else for my todo list).

Here's a slightly more complex example, with prettier contour labels:

    library (barsurf)
    library (KernSmooth)
    set.bs.theme ("heat")

    plot_ds <- function (dataset, main="", xlim, ylim, ...,
        ncontours=3, labcex=0.8, ndec=3,
        k1=1, k2=1, n=30)
    {   names <- names (dataset)
        x <- dataset [,1]
        y <- dataset [,2]
     bw.x <- k1 * bw.nrd (x)
        bw.y <- k2 * bw.nrd (y)
        if (missing (xlim) )
            xlim <- range (x) + c(-1, 1) * bw.x
        if (missing (ylim) )
            ylim <- range (y) + c(-1, 1) * bw.y

        ks <- bkde2D (dataset, c (bw.x, bw.y),
            c (n, n), list (xlim, ylim), FALSE)

        fb <- seq (min (ks$fhat), max (ks$fhat),
            length.out = ncontours + 2)
        fb <- fb [2:(ncontours + 1)]
        fb <- round (fb, ndec)

        plot_cfield (ks$x1, ks$x2, ks$fhat,
            main=main, xlab = names [1], ylab = names [2],
        points (x, y, pch=16, col="#00000040")
        contour (ks$x1, ks$x2, ks$fhat, levels=fb, labcex=labcex, add=TRUE)

    plot_ds (bat_call, "plot 2", c (25, 28), c (-15, 10), k1=1.25, k2=1.25)

If you still want a legend, have a look at:

And then modify the second half of my code, starting after ks <- ...

More information about the R-help mailing list