Triangular filled contour plot

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Triangular filled contour plot

kajo
Hi all,

I am working on a filled contour plot which shows a triangular matrix data set (as shown below). Is there a possibilty to draw a triangular filled contour in a equilateral triangle (like a ternary plot)?

Thanks in advance
Johannes

Reply | Threaded
Open this post in threaded view
|

Re: Triangular filled contour plot

Cleber N.Borges

hello,
maybe this code can be useful for you.

cleber
-----------------------


trimage <- function(f){
x = y = seq( 1, 0, l=181 )
t1 = length(x)
im = aux = numeric(0)
for( i in seq( 1, t1, by = 2 ) ){

     #idx = seq( t1**2, i*t1, by = -t1 ) - ((t1 - i):0)

     idx = seq( i*t1, t1**2, by = t1 ) - (i-1)
     im = c(im, aux, idx, aux )
     aux = c(aux, NA)
     }
z =  outer(X=x, Y=y, FUN=f)
return( matrix(z[im],nr=t1) )
}


### for chemical mixtures
### restriction:   sum( x[i]==1 ) and  0 < x[i] < 1
### naive example
f <- function(x1,x2) { x3=1-x2-x1;  -100*x1 + 0*x2 + 100*x3 }

windows(w=4.5, h=4.5, restoreConsole = TRUE )
par(mar=c(0,0,0,0), pty='s', xaxt='n', yaxt='n', bty='n' )

trimat <- trimage( f )
image( trimat )
contour( trimat, add=T)









Em 16/2/2010 11:25, kajo escreveu:

> Hi all,
>
> I am working on a filled contour plot which shows a triangular matrix data
> set (as shown below). Is there a possibilty to draw a triangular filled
> contour in a equilateral triangle (like a ternary plot)?
>
> Thanks in advance
> Johannes
>
> http://n4.nabble.com/file/n1557386/Bild3.png
>    


--
O bom senso é a coisa do mundo mais bem distribuída:
Todos pensamos tê-lo em tal medida que até os mais difíceis
de contentar nas outras coisas não costumam desejar mais bom senso do que aquele que têm.
[René Descartes]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Triangular filled contour plot

Walmes Zeviani
In reply to this post by kajo
Johannes,

Some months ago it was posted on R-help a list of packages that handle with ternaryplots, altough none of them can handle surfaceplots, just scatterplots, on a triangular area. The packages were

plot.acomp in compositions
tri in cwhmisc.cwhtool
triax in plotrix
ternary in StatDA
ternaryplot in vcd
ternaryplot in Zelig

See http://n4.nabble.com/triangular-plot-td893434.html#a893435

Due my needs I implemented something a little bit useful (at least to me) to make a prediction surface triangle plot using Lattice package. My reproducible code is the following:
paint <- data.frame(mono=c(17.5, 10, 15, 25, 5, 5, 11.25, 5, 18.13, 8.13, 25, 15, 10, 5),
                    cross=c(32.5, 40, 25, 25, 25, 32.5, 32.5, 40, 28.75, 28.75, 25, 25, 40, 25),
                    resin=c(50, 50, 60, 50, 70, 62.5, 56.25, 55, 53.13, 63.13, 50, 60, 50, 70),
                    hardness=c(29, 26, 17, 28, 35, 31, 21, 20, 29, 25, 19, 14, 30, 23))

pseudo <- with(paint, data.frame(mono=(mono-5)/(25-5), resin=(resin-50)/(70-50)))
pseudo$cross <- with(pseudo, 1-mono-resin)
pseudo$hardness <- paint$hardness

m1 <- lm(hardness~(mono+cross+resin)^2-mono, data=pseudo)

trian <- expand.grid(base=seq(0,1,l=100*2), high=seq(0,sin(pi/3),l=87*2))
trian <- subset(trian, (base*sin(pi/3)*2)>high)
trian <- subset(trian, ((1-base)*sin(pi/3)*2)>high)

new2 <- data.frame(cross=trian$high*2/sqrt(3))
new2$resin <- trian$base-trian$high/sqrt(3)
new2$mono <- 1-new2$resin-new2$cross

trian$yhat <- predict(m1, newdata=new2)

grade.trellis <- function(from=0.2, to=0.8, step=0.2, col=1, lty=2, lwd=0.5){
  x1 <- seq(from, to, step)
  x2 <- x1/2
  y2 <- x1*sqrt(3)/2
  x3 <- (1-x1)*0.5+x1
  y3 <- sqrt(3)/2-x1*sqrt(3)/2
  panel.segments(x1, 0, x2, y2, col=col, lty=lty, lwd=lwd)
  panel.text(x1, 0, label=x1, pos=1)
  panel.segments(x1, 0, x3, y3, col=col, lty=lty, lwd=lwd)
  panel.text(x2, y2, label=rev(x1), pos=2)
  panel.segments(x2, y2, 1-x2, y2, col=col, lty=lty, lwd=lwd)
  panel.text(x3, y3, label=rev(x1), pos=4)
}

levelplot(yhat~base*high, trian, aspect="iso", xlim=c(-0.1,1.1), ylim=c(-0.1,0.96),
          xlab=NULL, ylab=NULL, contour=TRUE,
          par.settings=list(axis.line=list(col=NA), axis.text=list(col=NA)),
          panel=function(..., at, contour=TRUE, labels=NULL){
            panel.levelplot(..., at=at, contour=contour, #labels=labels,
                            lty=3, lwd=0.5, col=1)
          })
trellis.focus("panel", 1, 1, highlight=FALSE)
panel.segments(c(0,0,0.5), c(0,0,sqrt(3)/2), c(1,1/2,1), c(0,sqrt(3)/2,0))
grade.trellis()
panel.text(0, 0, label="mono", pos=2)
panel.text(1/2, sqrt(3)/2, label="cross", pos=3)
panel.text(1, 0, label="resin", pos=4)
trellis.unfocus()

Result

I think (and hope :-) ) Deepayan Sarkar will implement this on Lattice soon because R doesn't have this kind of graphic yet. My code isn't perfect so I expect suggestions.

Walmes Zeviani, Brasil.
==========================================================================
Walmes Marques Zeviani
LEG (Laboratório de Estatística e Geoinformação, 25.450418 S, 49.231759 W)
Departamento de Estatística - Universidade Federal do Paraná
homepage: http://www.leg.ufpr.br/~walmes        twitter: @walmeszeviani
==========================================================================
Reply | Threaded
Open this post in threaded view
|

Re: Triangular filled contour plot

kajo
In reply to this post by kajo
Thank you for your recommendations and the submitted code.
That is excactly what I was searching for.
I am working on it to adapt it to my data set.

Best regards and thanks a lot
Johannes