[R] Persp and color

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Sep 16 16:32:03 CEST 2003

ucgamdo at ucl.ac.uk wrote:

> Hi, first of all I would like to say that honestly the persp demo is quite
> impresive, I won't take that away from you. The only problem I had, was
> that the code that actually builds the matrix of topo-colours that are used
> in the demo, is quite complicated (at least for me), and that code is
> poorly commented. So I was left with a series of help's() to try to see
> what each function would do, etc, etc..., while I was in that proccess I
> rembered about the wireframe function, and after checking its
> documentation, I found out that it has 'built-in' the ability to creaty
> this topo colors, that, I think is a great advantage. Maybe a good idea
> would be to insert the procedure you used to create the colors into the
> persp function itself, so humble neophyte users can easily plot striking
> volcano surfaces.
> This is actually the bit of code I couldn't work out, I know I would if I
> just could invest more of my precious time to it:
>>fcol <- fill
>>zi <- volcano[-1, -1] + volcano[-1, -61] + volcano[-87, 
>     -1] + volcano[-87, -61]


  [1] 87 61

you have to throw away some points of the margins, because you need 
(nx-1)*(ny-1) facets' colors. And you want the color to be specified for 
the middle of the facets, not one of the 4 corners, so you average the 
matrices of those 4 corners.

>>fcol[-i1, -i2] <- terrain.colors(20)[cut(zi, quantile(zi, 
>     seq(0, 1, len = 21)), include.lowest = TRUE)]

You use 20 different colors, choosen (indexed) by quantiles of the 
matrix calculated above. That's the obvious idea (nice implemented here, 

>>persp(x, y, 2 * z, theta = 110, phi = 40, col = fcol, 
>     scale = FALSE, ltheta = -120, shade = 0.4, border = NA, box = FALSE)
> just another thing, I have realised that the demo runs from beginning to
> end without stopping (not always), that is not very nice because the plots
> are displayed too quickly to appreciate, so the user is left to 'run' de
> demo manualy, i.e. copying and pasting each bit of code in order to see
> each plot in detail. I am aware that R is the product of the cooperation of
> many people, contributing part of their work-time into making it better, I
> think your demo is fine, and perhaps you won't have time to improve on it,
> don't worry about that (no bad feelings).

The improvement seems to be:

  par(ask = TRUE)

Uwe Ligges

> You correctly pointed out that a better way around was to go ask R-help
> directly. Certainly, that is what I intended to do with my own problem, but
> first I wanted to write my code properly. Tomorrow, I will post a thread
> about making persp representations of fractals in R, and maybe, you will be
> able to help me in showing how to correctly apply the colours to the
> surface. Maybe you will find this interested, and who knows, you will
> perhaps put it in your demo!
> By the way, I did also check help(persp), and how the colours are asigned
> to the surface facets is not well specified, not even on the examples (as I
> am aware of).
> Thanks,
> Mario.
> At 14:41 16/09/03 +0200, you wrote:
>>>>>>>"ucgamdo" == ucgamdo  <ucgamdo at ucl.ac.uk>
>>>>>>>    on Tue, 16 Sep 2003 11:46:18 +0100 writes:
>>   ucgamdo> Hi, If you run the demo for persp (I have R 1.7),
>>   ucgamdo> you will see that there is a good example of
>>   ucgamdo> 'coluring' a volcano according to different
>>   ucgamdo> heights, just try
>>   >> demo(persp)
>>   ucgamdo> and check out the code. You probably will find it
>>   ucgamdo> too complicated as I did, I was trying to do the
>>   ucgamdo> same and honestly I wasn't able to. 
>>Thank you for you honesty.  As a main author of the part of
>>demo(persp) I'm quite interested to find out what the problem
>>was.  I assume you also have looked at  help(persp) ?
>>   ucgamdo> However, there is a way around 
>>[ another way around would be to ask on R-help or ask  someone who
>> knows R better ... ... ]
>>   ucgamdo> and it is to use the
>>   ucgamdo> function wireframe from the lattice package
>>   >> library(lattice) ?wireframe
>>   ucgamdo> If you run through the help examples you'll see
>>   ucgamdo> that it is a lot easier to colour the surfaces the
>>   ucgamdo> way you want using this function. However,
>>   ucgamdo> wireframe is extreMELY slow, so, if you have a big
>>   ucgamdo> matrix it might be a pain in the behind. Also, the
>>   ucgamdo> way you feed the data to wireframe is different to
>>   ucgamdo> the way you do it with the persp function. I hope
>>   ucgamdo> this is of any help.
>>   ucgamdo> M.
>>   ucgamdo> ______________________________________________
>>   ucgamdo> R-help at stat.math.ethz.ch mailing list
>>   ucgamdo> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

More information about the R-help mailing list