R/Contour Plots

From QERM Wiki
(Difference between revisions)
Jump to: navigation, search
(added images, removed redundant code)
(added stuff from Carey and Bridget)
Line 35: Line 35:
 
  contour(x,y,z,levels=mylevels,add=T)
 
  contour(x,y,z,levels=mylevels,add=T)
  
Here is the full modified function,
+
Download the modified function {{Rcode|filled.contour2.R|filled.contour2.R}}
filled.contour2 <-
+
 
  function (x = seq(0, 1, length.out = nrow(z)),
+
[[Image:McGilliard figure.png | right | thumb | 300 px | Good contour plot]]
            y = seq(0, 1, length.out = ncol(z)), z, xlim = range(x, finite = TRUE),
+
Better yet, Carey McGilliard and Bridget Ferris made further modifications to allow multiple filled contours per page and the addition of the legend on the side to makes figures like the one at right.
            ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE),
+
{{Rcode|filled.contour3.R|filled.contour3.R}}
            levels = pretty(zlim, nlevels), nlevels = 20, color.palette = cm.colors,
+
{{Rcode|filled.legend.R|filled.legend.R}}
            col = color.palette(length(levels) - 1), plot.title, plot.axes,
+
 
            key.title, key.axes, asp = NA, xaxs = "i", yaxs = "i", las = 1,
+
 
            axes = TRUE, frame.plot = axes,mar, ...)
+
 
{
+
  # modification by Ian Taylor of the filled.contour function
+
  # to remove the key and facilitate overplotting with contour()
+
  if (missing(z)) {
+
    if (!missing(x)) {
+
      if (is.list(x)) {
+
        z <- x$z
+
        y <- x$y
+
        x <- x$x
+
      }
+
      else {
+
        z <- x
+
        x <- seq.int(0, 1, length.out = nrow(z))
+
      }
+
    }
+
    else stop("no 'z' matrix specified")
+
  }
+
  else if (is.list(x)) {
+
    y <- x$y
+
    x <- x$x
+
  }
+
  if (any(diff(x) <= 0) || any(diff(y) <= 0))
+
    stop("increasing 'x' and 'y' values expected")
+
  mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
+
  on.exit(par(par.orig))
+
  w <- (3 + mar.orig[2]) * par("csi") * 2.54
+
  par(las = las)
+
  mar <- mar.orig
+
  plot.new()
+
  par(mar=mar)
+
  plot.window(xlim, ylim, "", xaxs = xaxs, yaxs = yaxs, asp = asp)
+
  if (!is.matrix(z) || nrow(z) <= 1 || ncol(z) <= 1)
+
    stop("no proper 'z' matrix specified")
+
  if (!is.double(z))
+
    storage.mode(z) <- "double"
+
  .Internal(filledcontour(as.double(x), as.double(y), z, as.double(levels),
+
                          col = col))
+
  if (missing(plot.axes)) {
+
    if (axes) {
+
      title(main = "", xlab = "", ylab = "")
+
      Axis(x, side = 1)
+
      Axis(y, side = 2)
+
    }
+
  }
+
  else plot.axes
+
  if (frame.plot)
+
    box()
+
  if (missing(plot.title))
+
    title(...)
+
  else plot.title
+
  invisible()
+
}
+
  
 
==See also==
 
==See also==

Revision as of 23:55, 21 July 2010

Bad contour plot

Contour Plots

R has two built-in functions for contour plots, contour, and filled.contour. Examples of their use can be seen from the code below:

x <- 1:50
y <- 1:70
z <- matrix(expand.grid(x,y)$Var1^2 + expand.grid(x,y)$Var2^2,50,70)

# plain
contour(x,y,z) 

# adjusting levels
mylevels <- seq(0,7500,500) 
contour(x,y,z,levels=mylevels,xaxs='i',yaxs='i')

# filled contours
filled.contour(x,y,z,color.palette=heat.colors)
filled.contour(x,y,z,col=grey(seq(0,1,length=length(mylevels))))
# add this to line above and see bad match
contour(x,y,z,levels=mylevels,add=T)

However, it can be frustrating to realize that the way filled.contour was implemented does not allow removing the key or overplotting with lines and labels. To realize this, try the following:

filled.contour(x,y,z,col=grey(seq(0,1,length=length(mylevels))))
# add this to line above and see bad match
contour(x,y,z,levels=mylevels,add=T)
Good contour plot

A solution is to use the modified function, filled.contour2, as follows

# filled.contour function modified to not have key
filled.contour2(x,y,z,col=grey(seq(0.3,1,length=length(mylevels))))
# now we can successfully add lines and values
contour(x,y,z,levels=mylevels,add=T)

Download the modified function Rlogo.jpg filled.contour2.R

Good contour plot

Better yet, Carey McGilliard and Bridget Ferris made further modifications to allow multiple filled contours per page and the addition of the legend on the side to makes figures like the one at right. Rlogo.jpg filled.contour3.R Rlogo.jpg filled.legend.R



See also

R tips

Personal tools