# [R] Contour Plot on a non Rectangular Grid

Lorenzo Isella lorenzo.isella at gmail.com
Sun Oct 24 15:12:24 CEST 2010

```On 10/24/2010 02:55 PM, David Winsemius wrote:
>
> On Oct 24, 2010, at 4:30 AM, Lorenzo Isella wrote:
>
>> Dear All,
>> I would like to plot a scalar (e.g. a temperature) on a
>> non-rectangular domain (or even better: I would simply like to be able
>> to draw a contour plot on an arbitrary 2D domain). I wonder if there
>> is any tool to achieve that with R. I did some online search in
>> particular on the list archives, found several queries similar to this
>> one but was not able to find any conclusive answer.
>
> One implemented approach to this exists with the rms/Hmisc package
> combination. The perimeter function is used to define a region within
> which the are a sufficient number of cases and the perimeter object is
> passed to the bplot function, which is a wrapper for a lattice
> contourplot call. There is no reason you couldn't emulate
>
>
>> I am interested in the following 2 options
>>
>> (1) just read a file of the form
>>
>> x1 y1 z1
>> x2 y2 x2
>> ... ... ...
>> xn yn zn
>>
>> where the set of {xi} and {yi} are coordinates on an arbitrary domain
>> and {zi} are the values of the scalar for the corresponding {x,y}
>> coordinates.
>> (2) Sometimes the domain where I want to draw a contour plot is
>> nothing too fancy and the scalar itself is given by an analytical
>> function. Consider e.g. the case of a circle of radius R=pi/2 centered
>> about the origin and a function like
>>
>> z=f(x,y)=abs(cos(y))
>>
>
> That defines the contours but does not restrict the domain.
>
>> NB: in this case a satisfactory solution could be to plot z on a
>> rectangular grid and then clip a circular region
>
>> To fix the ideas, the final result in this case (with a colorjet map)
>> should look like this
>>
>> http://dl.dropbox.com/u/5685598/scalar_plot.pdf
>
> And that color encoded output would not be the output of a contourplot
> but is more like a levelplot or an image plot. Nonetheless, the
> perimeter and bplot combination can deliver a similar result if you
> supply either code or data as a suitable test case for analysis and
> display.
>
>

I agree that contour plot was a misleading name for what I had in mind.
I'll try your suggestion and the one by Uwe about rgl and post again if
I had troubles. As to the domain of the function, at least in case (1),
that should arise from the collected data points in (x,y) if the
sampling is dense enough.
Cheers

Lorenzo

```