On 22/11/2009 6:28 PM, (Ted Harding) wrote:
Wow! (Top-posting for once, since there's no natural other place ...)
Thanks Peter, David and Duuncan for the suggestions. I'll look at the
later ones from David & Duncan later (it's getting late here).
However, as something to work on if you want to, here is a toy
example, based on the same overall methodology as I'm using for
my real plot (though the real one looks quite different):
#############
library(MASS)
set.seed(54321)
X <- rnorm(100) ; Y <- rnorm(100)
h0 <- 2.0
W <- kde2d(x=X,y=Y,n=100,h=c(h0,h0),lims=c(-3,3,-3,3))
Wmax <- max(W$z)
W$z <- 10*(W$z/Wmax)
Palette <- colorRampPalette(c("lightgreen","red"),
interpolate="spline" )
Levels <- (1.0*(0:10))
filled.contour(x=W$x,y=W$y,z=W$z,levels=Levels,
xlim=c(-3,3),ylim=c(-3,3),
color.palette=Palette,
plot.axes={axis(1);axis(2);points(X,Y,pch="+",col="blue");
points(X,Y,pch="+",col="blue");
contour(x=W$x,y=W$y,z=W$z,levels=Levels,labcex=1.5,
col="red",add=TRUE, labels=" ", method="flattest"
);
contour(x=W$x,y=W$y,z=W$z,levels=Levels,labcex=1.5,
lty=0,col="black",add=TRUE, method="flattest"
);
}
)
#############
I've incorporated here the first two suggestiong from Peter and
David, though not (yet) any of the later ones. One thing that emerges
is that the two contour() plots don't quite match up as to where
(or whether) the space from 'labels=" "' in the first are made,
and the placing of the labels in the second.
I'll try the other suggestions and see what happens -- but I may
end up doing the first plot (red) without labels, so that there's
no break in the contours. Better, I think, to have the black labels
simply stuck onto the red contours, than have some of them misaligned
with bvreaks in the contours.
Here's a version of the last one I posted:
filled.contour(x=W$x,y=W$y,z=W$z,levels=Levels,
xlim=c(-3,3),ylim=c(-3,3),
color.palette=Palette,
plot.axes={axis(1);axis(2);points(X,Y,pch="+",col="blue");
points(X,Y,pch="+",col="blue");
reps <- round(strwidth(Levels, cex=1.5) / strwidth(" ",
cex=1.5))
spaces <- sapply(reps, function(x) paste(rep(" ", round(x)),
collapse=""))
contour(x=W$x,y=W$y,z=W$z,labels=spaces,levels=Levels,labcex=1.5,
col="red",add=TRUE, method="flattest", drawlabels=TRUE
);
contour(x=W$x,y=W$y,z=W$z,levels=Levels,labcex=1.5,
lty=0,col="black",add=TRUE, method="flattest"
);
}
)
Duncan Murdoch
Thank you all!
Ted.
On 22-Nov-09 22:53:03, David Winsemius wrote:
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)
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?
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.
I see nothing in ?contour nor in ?par about this.
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.hard...@manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Nov-09 Time:
17:06:08
------------------------------ XFMail
------------------------------
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Nov-09 Time: 23:28:19
------------------------------ XFMail ------------------------------
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.