[R] Triangular filled contour plot

Walmes Zeviani walmeszeviani at hotmail.com
Tue Feb 16 19:25:00 CET 2010


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()

http://n4.nabble.com/file/n1557735/one.png 

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.

-----
..ooo0
...................................................................................................
..(....)... 0ooo...                              Walmes Zeviani
...\..(.....(.....)...     Master in Statistics and Agricultural
Experimentation
....\_)..... )../....       walmeszeviani at hotmail.com, Lavras - MG, Brasil
............
(_/............................................................................................
-- 
View this message in context: http://n4.nabble.com/Triangular-filled-contour-plot-tp1557386p1557735.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list