# [R] Contour Plot on a non Rectangular Grid

David Winsemius dwinsemius at comcast.net
Mon Oct 25 01:32:23 CEST 2010

```On Oct 24, 2010, at 9:30 AM, Lorenzo Isella wrote:

>
>>> Hi,
>>> And thanks for helping. I am anyway a bit puzzled, since case (1)
>>> is not
>>> only a matter of interpolation. Probably the point I did not make
>>> clear
>>> (my fault) is that case (1) in my original email does not refer to
>>> an
>>> irregular grid on a rectangular domain; the set of (x,y) coordinate
>>> could stand e.g. a flat metal slab along which I have temperature
>>> measurements. The slab could be e.g. elliptical or any other funny
>>> shape. What also matters is that the final outcome should not look
>>> rectangular, but by eye one should be able to tell the shape of
>>> the slab.
>>> Case (1) is a generalization of case (2) where I do not have
>>> either an
>>> analytical expression for the surface not for the scalar.
>>> Cheers
>>
>>
>> What about the facilities in package rgl then?
>>
>> Uwe Ligges
>
> Hello,
> I feel I am drowning in a glass of water.

Not sure what we are supposed to make of this.

> Consider the following snippet at the end of the email, where I
> generated a set of {x,y,s=f(x,y)} values, i.e. a set of 2D
> coordinates + a scalar on a circle.
> Now, I can get a scatterplot in 3D, but how to get a 2D surface plot/
> levelplot?

You were advised to look at rms. Why have you dismissed this
suggestion? Using your data setup below and packaging into a dataframe.

require(rms)
olsfit <- ols(V3~rcs(V1,3)+rcs(V2,3), data=xysf)

bounds <- perimeter(xysf\$V1, xysf\$V2)
plot(xysf\$V1, xysf\$V2) #demonstrates the extent of the data
bplot(Predict(olsfit, V1,V2),  perim=bounds)  # a levelplot is the
default

bplot(Predict(olsfit, V1,V2),  perim=bounds, lfun=contourplot)
bplot(Predict(olsfit, V1,V2),  perim=bounds, lfun=contourplot,
xlim=c(-2.5,2.5))
# to demonstrate that perimeter works

# and as expected this shows very little variability d/t V1
olsfit  # note that
> anova(olsfit)
Analysis of Variance          Response: V3

Factor          d.f. Partial SS   MS           F          P
V1                 2   0.01618738 8.093691e-03      19.47 <.0001
Nonlinear         1   0.01618738 1.618738e-02      38.93 <.0001
V2                 2 470.67057254 2.353353e+02  566040.95 <.0001
Nonlinear         1 470.67057254 4.706706e+02 1132081.91 <.0001
TOTAL NONLINEAR    2 527.78127558 2.638906e+02  634723.80 <.0001
REGRESSION         4 527.78127558 1.319453e+02  317361.90 <.0001
ERROR           7663   3.18594315 4.157566e-04
# most the the regression SS is in the V2 variable
# Q.E.D.
--
David,

> An idea could be to artificially set the z coordinate of the plot as
> a constant (instead of having it equal to s as in the scatterplot)
> and calculate the colormap with the values of s, along the lines of
> the volcano example + surface plot at
>
> http://bit.ly/9MRncd
>
> but I am experiencing problems. However, should I really go through
> all this? There is nothing truly 3D in the plot that I have in mind,
> you can think of it as e.g. some temperature measurement along a
> tube cross section.
> Any help is appreciated.
> Cheers
>
> Lorenzo
>
> ########################################
> library(scatterplot3d)
> library(rgl)
> R <- pi/2
>
> n <- 100
>
> x <- y <- seq(-R,R, length=n)
>
> xys <- c()
>
> temp <- seq(3)
>
> for (i in seq(n)){
>
>     for (j in seq(n))
>
> #check I am inside the circle
>        if ((sqrt(x[i]^2+y[j]^2))<=R){
>
>       temp[1] <- x[i]
>       temp[2] <- y[j]
>       temp[3] <- abs(cos(y[j]))
>      xys <- rbind(xys,temp)
>  }
> }
> scatterplot3d(xys[,1], xys[,2], xys[,3], highlight.3d=TRUE,
> col.axis="blue", col.grid="lightblue",
> main="scatterplot3d - 2", pch=20)
>
>
>
> #########################################################

```