Roberto | 26 Dec 08:58 2012
Picon

3D Gaussian with different colors

I wrote, finding pieces of code in the web, a simple script to draw a 3D
gaussian plot.
For the next step, I need that the 2 gaussian distributions have different
colors.

Can someone help me to do this?

fn <- function(x, y, scale, scale2)
dnorm(x,mean=1,sd=scale)*dnorm(y,mean=-1,sd=scale) +
dnorm(x,mean=2,sd=scale2)*dnorm(y,mean=1,sd=scale2)

x <- seq(-4,4,len=40)
y <- seq(-4,4,len=40)
z <- outer(x, y, fn, scale = 1, scale2 = 0.5)
nrz <- nrow(z)
ncz <- ncol(z)
jet.colors <- colorRampPalette( c("lightblue", "green", "darkgreen") )
nbcol <- 100
color <- jet.colors(nbcol)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)
persp(x,y,z, theta = 30, phi = 30, expand = 0.2, col = color[facetcol],
      ltheta = 120, shade = 0.75, ticktype = "detailed") 

--
View this message in context: http://r.789695.n4.nabble.com/3D-Gaussian-with-different-colors-tp4653938.html
Sent from the R help mailing list archive at Nabble.com.

(Continue reading)

David Winsemius | 26 Dec 20:37 2012
Picon
Picon

Re: 3D Gaussian with different colors


On Dec 25, 2012, at 11:58 PM, Roberto wrote:

> I wrote, finding pieces of code in the web, a simple script to draw  
> a 3D
> gaussian plot.

> For the next step, I need that the 2 gaussian distributions have  
> different
> colors.
>
> Can someone help me to do this?
>
> fn <- function(x, y, scale, scale2)
> dnorm(x,mean=1,sd=scale)*dnorm(y,mean=-1,sd=scale) +
> dnorm(x,mean=2,sd=scale2)*dnorm(y,mean=1,sd=scale2)
>
> x <- seq(-4,4,len=40)
> y <- seq(-4,4,len=40)
> z <- outer(x, y, fn, scale = 1, scale2 = 0.5)
> nrz <- nrow(z)
> ncz <- ncol(z)
> jet.colors <- colorRampPalette( c("lightblue", "green", "darkgreen") )
> nbcol <- 100
> color <- jet.colors(nbcol)
> zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
> # Recode facet z-values into color indices
> facetcol <- cut(zfacet, nbcol)
> persp(x,y,z, theta = 30, phi = 30, expand = 0.2, col =  
> color[facetcol],
(Continue reading)

Roberto | 27 Dec 15:09 2012
Picon

Re: 3D Gaussian with different colors

Hi,
thank you very much for your help, but I need something different.

For example, I wrote this code to highlight the standard deviation area 
of each distribution

fn <- function(x, y, scale, scale2)
dnorm(x,mean=1,sd=scale)*dnorm(y,mean=-1,sd=scale) +
dnorm(x,mean=2,sd=scale2)*dnorm(y,mean=1,sd=scale) +
dnorm(x,mean=-3,sd=scale)*dnorm(y,mean=1,sd=scale2)

x <- seq(-4,4,len=40)
y <- seq(-4,4,len=40)
z <- outer(x, y, fn, scale = 1, scale2 = 0.5)
nrz <- nrow(z)
ncz <- ncol(z)
jet.colors <- colorRampPalette( c("lightblue", "yellow") )
nbcol <- 100
color <- jet.colors(nbcol)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)

phi <- seq(0, 2*pi, len = 201)

#postscript ("3d-gaussian.eps", family = "Times", paper="special", 
horizontal=FALSE, width=15/2.54, height=15/2.54, pointsize=8)
res <- persp(x,y,z, theta = 30, phi = 30, expand = 0.2, col = 
color[facetcol],
       ltheta = 120, shade = 0.75, ticktype = "detailed")
(Continue reading)


Gmane