# [R] how to plot three dimension data to filled contour plot or surface plot in R Ask Question

Ismail SEZEN sezenismail at gmail.com
Tue Apr 11 18:48:42 CEST 2017

```After lon long search, my best shot was to use filled.contour + lattice::levelplot together [a] to represent 3 dimensional data on a flat surface.

a) http://r.789695.n4.nabble.com/Creating-smooth-color-regions-with-panel-contourplot-td866253.html <http://r.789695.n4.nabble.com/Creating-smooth-color-regions-with-panel-contourplot-td866253.html>

Some Details:

> On 11 Apr 2017, at 09:16, dncdd via R-help <r-help at r-project.org> wrote:
>
> ENV
>
> R 3.3.2
>
> When I have data like:
>
>     rdn<-c(0.8,1.8,2.8)
>     tdn<-c(1,2,3,4,5,6,7,8,9)
>
>     idn<-matrix(c(0.3, 0.3, 0.3, 0.2, 0.2, 0.4, 0.1, 0.1, 0.5, 0, 0.2, 0.5, 0, 0.3, 0.6, 0, 0.4, 0.6, 0, 0.4, 0.6, 0, 0.5, 0.7, 0, 0.5, 0.7), nrow=9, ncol=3, byrow=T)
>
> And the matrix looks like(3*9 = 27 data elements):
>
>     0.3, 0.3, 0.3,
>     0.2, 0.2, 0.4,
>     0.1, 0.1, 0.5,
>     0, 0.2, 0.5,
>     0, 0.3, 0.6,
>     0, 0.4, 0.6,
>     0, 0.4, 0.6,
>     0, 0.5, 0.7,
>     0, 0.5, 0.7
>
> Then I can get a filled.contour with parameters x,y,z. x is tdn, y is rdn, z is the matrix. I already get this.

If you have a rectangular data like matrix, you can use filled.contour (the best one) or rasterVis::levelplot.

>
>
> **My current problem** is:
>
> What If I have three dimension data
>
>     r1dn<-c(0.8,1.8,2.8)
>     r2dn<-c(0.8,1.8,2.8)
>     tdn<-c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)

At this point, you have to expand your data to x,y,z data pairs. I mean you have to reshaphe your data as a z value for each x and y point.

data <- expand.grid(x = x, y = y)
dta\$z <- z

Now, you have 3 columns + 9 rows data. You can use this data in lattice::levelplot by awesome formula interface.

>
> I googled many surface and contour codes but I still not find some code for three dimension data yet.

Actually you found but they didn’t fill you requirements.

> How to do that in R? Say, x is r1dn, y is r2dn, z is tdn, what about the three dimension data? Does ggplot can plot three dimension filled contour or surface plot? Or another alternative solutions?

The problem here is, you want a filled/contour plot on a flat surface or 3D perspective visualisation? If you want 3D perspective visualisation, your only chances are rgl and misc3d packages. (as far as I know)

>
> All I expected is a 3d plot with color changes smoothly and no grid on it.

All the solutions above may have (or not) smooth colour change and grid depending on your settings. What do you mean exactly "no grid on it”? If you choose that you don’t have a grid, then you don’t. You only need to search a bit more.

>
> Looks like:
> no grid for next three figures
> Those should be 3d filled contour or 3d surface plot.
>
>   [1]: https://i.stack.imgur.com/z6u3p.png
>   [2]: https://i.stack.imgur.com/MEnFn.png
>   [3]: https://i.stack.imgur.com/Ri29w.png
>   [4]: https://i.stack.imgur.com/CdCqL.jpg
>   [5]: https://i.stack.imgur.com/Pt1Nw.jpg
> 	[[alternative HTML version deleted]]

[[alternative HTML version deleted]]

```