Marius Hofert
2011-Dec-31 11:02 UTC
[R] How to color a region in a contour plot with the contour being the boundary?
Dear expeRts, I would like to color a certain region in a levelplot. The region for z <= 0.02 should have a dark gray color. Below is a minimal example. It almost does what I want, but The region between z=0.02 and z=1 is also colored in dark gray (instead of just the region for z <= 0.02). How can I solve this? Cheers, Marius ## z values for given x and y f <- function(x) 4*((1-x)^(-1/2)-1) eps <- 1e-12 x <- y <- seq(eps, 1-eps, length.out=500) z <- outer(x, y, FUN=function(x, y) pmax(f(x) + f(y) - 10, 0)) ## determine colors zcols <- c(gray(0.2), gray(seq(0.5, 1, length.out=50)), rep("#FFFFFF", max(z)-51)) # colors with dark gray in the beginning for z <= 0.02 and many whites for z > 50 ## trial 1 pdf(file="levelplot1.pdf", width=6, height=6) image(x, y, z, xaxs="r", yaxs="r", col=zcols) contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) dev.off() ## trial 2 pdf(file="levelplot2.pdf", width=6, height=6) image(x, y, z, xaxs="r", yaxs="r", col=zcols, oldstyle=TRUE) contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) dev.off()
Jean V Adams
2012-Jan-03 18:06 UTC
[R] How to color a region in a contour plot with the contour being the boundary?
Try the levelplot() function in package lattice. library(lattice) dat <- expand.grid(x=x, y=y) dat$z <- pmax(f(dat$x) + f(dat$y) - 10, 0) levelplot(z ~ x * y, dat, at=c(-1, 0.02, 1, 5, 10, 20, 50, 500, 9000000), labels=TRUE, contour=TRUE, colorkey=FALSE, col.regions=gray(c(0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1))) Jean Marius Hofert wrote on 12/31/2011 05:02:22 AM:> Dear expeRts, > > I would like to color a certain region in a levelplot. The region > for z <= 0.02 should have a dark gray color. Below is a minimal > example. It almost does what I want, but The region between z=0.02 > and z=1 is also colored in dark gray (instead of just the region for > z <= 0.02). > How can I solve this? > > Cheers, > > Marius > > > ## z values for given x and y > f <- function(x) 4*((1-x)^(-1/2)-1) > eps <- 1e-12 > x <- y <- seq(eps, 1-eps, length.out=500) > z <- outer(x, y, FUN=function(x, y) pmax(f(x) + f(y) - 10, 0)) > > ## determine colors > zcols <- c(gray(0.2), gray(seq(0.5, 1, length.out=50)), rep > ("#FFFFFF", max(z)-51)) # colors with dark gray in the beginning for > z <= 0.02 and many whites for z > 50 > > ## trial 1 > pdf(file="levelplot1.pdf", width=6, height=6) > image(x, y, z, xaxs="r", yaxs="r", col=zcols) > contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) > dev.off() > > ## trial 2 > pdf(file="levelplot2.pdf", width=6, height=6) > image(x, y, z, xaxs="r", yaxs="r", col=zcols, oldstyle=TRUE) > contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) > dev.off()[[alternative HTML version deleted]]
Marius Hofert
2012-Jan-03 23:45 UTC
[R] How to color a region in a contour plot with the contour being the boundary?
I was hoping for a solution without lattice. But it solves the problem, thanks Jean. Cheers, Marius On 2012-01-03, at 19:06 , Jean V Adams wrote:> > Try the levelplot() function in package lattice. > > library(lattice) > > dat <- expand.grid(x=x, y=y) > dat$z <- pmax(f(dat$x) + f(dat$y) - 10, 0) > > levelplot(z ~ x * y, dat, > at=c(-1, 0.02, 1, 5, 10, 20, 50, 500, 9000000), > labels=TRUE, contour=TRUE, colorkey=FALSE, > col.regions=gray(c(0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1))) > > Jean > > > Marius Hofert wrote on 12/31/2011 05:02:22 AM: > > > Dear expeRts, > > > > I would like to color a certain region in a levelplot. The region > > for z <= 0.02 should have a dark gray color. Below is a minimal > > example. It almost does what I want, but The region between z=0.02 > > and z=1 is also colored in dark gray (instead of just the region for > > z <= 0.02). > > How can I solve this? > > > > Cheers, > > > > Marius > > > > > > ## z values for given x and y > > f <- function(x) 4*((1-x)^(-1/2)-1) > > eps <- 1e-12 > > x <- y <- seq(eps, 1-eps, length.out=500) > > z <- outer(x, y, FUN=function(x, y) pmax(f(x) + f(y) - 10, 0)) > > > > ## determine colors > > zcols <- c(gray(0.2), gray(seq(0.5, 1, length.out=50)), rep > > ("#FFFFFF", max(z)-51)) # colors with dark gray in the beginning for > > z <= 0.02 and many whites for z > 50 > > > > ## trial 1 > > pdf(file="levelplot1.pdf", width=6, height=6) > > image(x, y, z, xaxs="r", yaxs="r", col=zcols) > > contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) > > dev.off() > > > > ## trial 2 > > pdf(file="levelplot2.pdf", width=6, height=6) > > image(x, y, z, xaxs="r", yaxs="r", col=zcols, oldstyle=TRUE) > > contour(x, y, z, levels=c(0.02, 1, 5, 10, 20, 50, 500), add=TRUE) > > dev.off()