[R] contour(): lines & labels in different colours?
Duncan Murdoch
murdoch at stats.uwo.ca
Sun Nov 22 23:47:36 CET 2009
On 22/11/2009 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)
>>> contour(z, lty=0, labcex=1, add=TRUE)
>> 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)
>> contour(z, lty=0, labcex=1.5, add=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?
Here's a little implementation. It didn't work using different cex
values for the spaces and the levels, but this seems okay:
x <- -6:16
z <- outer(x,x)
levels <- pretty(range(z, finite=TRUE), 10)
plot.new() # Might want a throwaway plot instead
reps <- round(strwidth(levels, cex=1.5) / strwidth(" ", cex=1.5))
spaces <- sapply(reps, function(x) paste(rep(" ", round(x)), collapse=""))
contour(z, labels=spaces, levels=levels, col=2, labcex=1.5, drawlabels=TRUE)
contour(z, lty=0, labcex=1.5, add=TRUE)
Duncan Murdoch
More information about the R-help
mailing list