R/Contour Plots

From QERM Wiki
(Difference between revisions)
Jump to: navigation, search
(added stuff from Carey and Bridget)
(moved formatting around)
Line 1: Line 1:
[[Image:Filled_contour_bad.png | right | thumb | 300 px | Bad contour plot]]
 
 
 
=Contour Plots=
 
=Contour Plots=
 +
 +
[[Image:Filled_contour_bad.png | left | thumb | 300 px | Bad contour plot]]
 +
[[Image:Filled_contour_good.png | center | thumb | 300 px | Good contour plot]]
 +
 +
 
R has two built-in functions for contour plots, contour, and filled.contour. Examples of their use can be seen from the code below:
 
R has two built-in functions for contour plots, contour, and filled.contour. Examples of their use can be seen from the code below:
  
Line 21: Line 24:
 
  contour(x,y,z,levels=mylevels,add=T)
 
  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:
+
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, add the lines below to the code above to create the bad figure above left.
  
 
  filled.contour(x,y,z,col=grey(seq(0,1,length=length(mylevels))))
 
  filled.contour(x,y,z,col=grey(seq(0,1,length=length(mylevels))))
Line 27: Line 30:
 
  contour(x,y,z,levels=mylevels,add=T)
 
  contour(x,y,z,levels=mylevels,add=T)
  
[[Image:Filled_contour_good.png | right | thumb | 300 px | Good contour plot]]
 
  
A solution is to use the modified function, filled.contour2, as follows
+
A solution is to use the modified function, {{Rcode|filled.contour2.R|filled.contour2.R}}, as follows to create the better figure above right.
 
  # filled.contour function modified to not have key
 
  # filled.contour function modified to not have key
 
  filled.contour2(x,y,z,col=grey(seq(0.3,1,length=length(mylevels))))
 
  filled.contour2(x,y,z,col=grey(seq(0.3,1,length=length(mylevels))))
Line 35: Line 37:
 
  contour(x,y,z,levels=mylevels,add=T)
 
  contour(x,y,z,levels=mylevels,add=T)
  
Download the modified function {{Rcode|filled.contour2.R|filled.contour2.R}}
 
  
[[Image:McGilliard figure.png | right | thumb | 300 px | 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.
 
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.
 
{{Rcode|filled.contour3.R|filled.contour3.R}}
 
{{Rcode|filled.contour3.R|filled.contour3.R}}
 
{{Rcode|filled.legend.R|filled.legend.R}}
 
{{Rcode|filled.legend.R|filled.legend.R}}
  
 +
[[Image:McGilliard figure.png | center | thumb | 300 px | Good contour plot]]
  
  

Revision as of 00:00, 22 July 2010

Contour Plots

Bad contour plot
Good contour plot


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, add the lines below to the code above to create the bad figure above left.

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)


A solution is to use the modified function, Rlogo.jpg filled.contour2.R, as follows to create the better figure above right.

# 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)


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

Good contour plot


See also

R tips

Personal tools