[R] contour plots

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jun 22 18:23:26 CEST 2010


On 22/06/2010 9:29 AM, dbeest wrote:
> Hi All,
>
> I'm having difficulty making a contour plot a would like some help.
>
> A standard contourplot can be made by having an x,y, and some matrix (shape
> x*y) with contents:
>
> x = 1:10
> y = 1:10
> cont <- matrix(runif(100,min=1,max=2),nrow=10,ncol=10)
> filled.contour(x,y,cont)
>
> Looks very nice.
>
>
> Sometimes data comes in a different format however. Basically an x and an y,
> and a vector z with all realisations of some f(x,y). The only R way of
> making a contour plot out of this I can find is: 
>   

You should interpolate the data to a grid, and use the first method.  
The interp function in the akima package can do this.  For example,
> library(lattice)
> x = runif(100,min=0,max=10)
> y = runif(100,min=0,max=10)
> z = runif(100,min=0,max=2)
> contourplot(z ~ x * y, region = TRUE)
>   

library(akima)
grid <- interp(x,y,z)
filled.contour(grid)

Duncan Murdoch

> which is pretty ugly. And I don't think it needs to be. Does anyone know a
> way to make plots of the first type with data of the second type?
>
> Increasing the number of points to see if that fits better:
> x = runif(1000,min=0,max=10)
> y = runif(1000,min=0,max=10)
> z = runif(1000,min=0,max=2)
> contourplot(z ~ x * y, region = TRUE)
>
> ...hang on that doesn't plot anything: not my main question, but if anyone
> cares to explain why that is..
>
> cheers,
>
> Dennis
>
>



More information about the R-help mailing list