Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filled.contour - delineate the change in the z value levels

Tags:

r

graphics

I have to produce filled.contour plots that have some kind of symmetry in the z-values like in the below code

x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
filled.contour(x,y,z)
filled.contour(x,y,z,color.palette=rainbow)
z2 <- z
z2[5,5] <- Inf
filled.contour(x,y,z2,col=rainbow(200),nlevels=200)

I would like to be able to delineate the levels of the z value for example certain levels or increments (2,4,6,8,10) increments so as to obtain the following plot

enter image description here

I want to leave the levels of col=rainbow(200) in order to plot smooth colors and color transitions but I want to delineate certain values/increments.

Is this possible? I would appreciate a base R solution but in case this is not possible any solution would be good (ggplot, lattice)

like image 476
ECII Avatar asked Oct 11 '25 21:10

ECII


1 Answers

You can use countourLines() to add the lines and text() to add the text:

txtlab <- c("2", "4", "6", "8", "Inf")
txtpos <- c(1.25, 2, 3, 4, 4.75)
ctlns <- contourLines(x, y, z2, levels=c(3, 5, 7, 9))
filled.contour(x,y,z2,col=rainbow(200),nlevels=200, 
     plot.axes={axis(1); axis(2); text(txtpos, txtpos, txtlab, cex=1.5);
     sapply(1:4, function(x) lines(ctlns[[x]][[2]], ctlns[[x]][[3]], lwd=2))
})

Filled contour

like image 131
dcarlson Avatar answered Oct 14 '25 16:10

dcarlson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!