Skip to content
Prev 201239 / 398502 Next

contour(): lines & labels in different colours?

On Nov 22, 2009, at 8:01 PM, Duncan Murdoch wrote:

            
I thought the code was good, although there is a duplicated points ()  
call, but the offered colorPallete would be improved by going to  
lightyellow to blue and the points to orange. Also got "bold" labels  
to work. Still takes a bit of experimentation with the width of the  
space string when you vary the number of points for some reason:

library(MASS)
set.seed(54321)
X <- rnorm(500) ; Y <- rnorm(500)
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("lightyellow","blue"),
                            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="orange", cex=.8);
             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",vfont=c("sans serif", "bold"),  
add=TRUE, method="flattest"
                    );
            }
)
David Winsemius, MD
Heritage Laboratories
West Hartford, CT