# [R] contour(): lines & labels in different colours?

David Winsemius dwinsemius at comcast.net
Sun Nov 22 23:53:03 CET 2009

```On Nov 22, 2009, at 5:35 PM, Duncan Murdoch wrote:

> On 22/11/2009 5:21 PM, David Winsemius wrote:
>> On Nov 22, 2009, at 4:57 PM, Peter Ehlers wrote:
>>> Hi Ted,
>>>
>>> This won't solve your problem, but a small improvement might
>>> be to place the labels over the lines rather than the other
>>> way around. It will definitely avoid putting red lines over
>>> black ones:
>>>
>>> x <- -6:16
>>> z <- outer(x,x)
>>> contour(z, labels="", col=2)
>> I played around a bit with you example, and can get almost the
>> desired  color and lack of cutting through labels. There is the
>> possibility of  plotting empty labels that create a space in the
>> curves for the later  labels-without-lines overlay:
>> x <- -6:16
>> z <- outer(x,x)
>> contour(z, labels="    ", col=2, labcex=1.5, drawlabels=TRUE)
>
> That's a nice solution.  You could probably do a bit better in a
> couple of steps:  1st, figure out what the level labels will be (by
> default, pretty(range(z, finite=TRUE), 10) ), then compute an
> equivalent number of spaces, e.g.
>
> levels <- pretty(range(z, finite=TRUE), 10)
> strwidth(levels, cex=1.5) / strwidth(" ", cex=0.5)
>
> Then use the appropriate number of spaces as the labels in the first
> plot, and the numbers in the second one.  Do we have a simple
> function to take input like c(10, 12) and produce two character
> strings containing 10 and 12 spaces?
>

Not sure it is "simple" but this (after more playing around) did the
trick:

library(R.oo)
vecspaces <- function(n) sapply(n, function(x)
paste(rep(intToChar(32), x), sep="", collapse="") )

> vecspaces(c(10,12) )
[1] "          "   "            "

> vecspaces(1:10)
[1] " "          "  "         "   "        "    "       "     "
"      "     "       "
[8] "        "   "         "  "          "

--
David

> Duncan Murdoch
>
>>> Cheers,
>>> Peter
>>>
>>>
>>> (Ted Harding) wrote:
>>>> Greetings, All!
>>>> I want to draw contour lines in red, using contour(), but also
>>>> have the contour labels (for the level-values) in black so that
>>>> they will stand out against a coloured background already generated
>>>> using filled.contour() (the background shades from green at low
>>>> levels of "risk" to red at high levels).
>>>> In any case, contour labels in red are already somewhat
>>>> inconspicuous
>>>> with contour lines in red, regardless of background.
>>>> One way to approach it could be to first draw the labelled contours
>>>> in black, and then overlay by re-drawing (with out labels) in red.
>>>> This would sort-of work, but the red contour lines would then cut
>>>> through the black numbers, which is somewhat undesirable. Also
>>>> (I've tried it) you can get show-through along the contour lines
>>>> from the black layer, which is nasty.
>>>> Any suggestions?
>>>> With thanks,
>>>> Ted.
>>>> --------------------------------------------------------------------
>>>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>>>> Fax-to-email: +44 (0)870 094 0861
>>>> Date: 22-Nov-09                                       Time:
>>>> 17:06:08
>>>> ------------------------------ XFMail
>>>> ------------------------------
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help