Suppose I have three numbers p1, p2, p3 with 0 <= p1,p2,p3 <= 1 and p1+p2+p3=1, and a function f=f(p1,p2,p3) = f(p1,p2,1-p1-p2). How to draw a contour plot of f() on the p1+p2+p3=1 plane, that is, an equilateral triangle? Functions triplot(), triangle.plot(), and ternaryplot() give only scatterplots, AFAICS -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
On 6/18/2007 8:12 AM, Robin Hankin wrote:> Suppose I have three numbers p1, p2, p3 with > 0 <= p1,p2,p3 <= 1 and p1+p2+p3=1, > and a function f=f(p1,p2,p3) = f(p1,p2,1-p1-p2). > > How to draw a contour plot of f() on the p1+p2+p3=1 plane, > that is, an equilateral triangle?The usual contour function leaves blanks where you give it NA values, so you could put the f values into a rectangular array with NA outside the triangle and use that. I don't know how you're thinking of displaying things, but one possible transformation from (x,y) to (p1, p2, p3) would be f <- function(p1, p2, p3) p3 # just to illustrate maxy <- sin(pi/3) x <- seq(0,1,len=100) y <- seq(0, maxy, len=100) p1 <- outer(x,y, function(x,y) x - y/maxy/2) p2 <- outer(x,y, function(x,y) y/maxy) p3 <- 1-p1-p2 z <- ifelse(0 < p1 & 0 < p3, f(p1,p2,p3), NA) contour(x,y,z) This puts p1==1 at the bottom right, p2==1 at the top, and p3==1 at the bottom left. Duncan Murdoch> > Functions triplot(), triangle.plot(), and ternaryplot() give > only scatterplots, AFAICS > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
The triplot function in the TeachingDemos package (I don't know about the one in klaR, or the others mentioned) honors the type='l' argument and passes it on to points. So if you know where you want the contours drawn, you can use triplot to draw the lines (it also has an add argument that could be used to add labels after plotting the lines). You can also look at the source code to see how the plotting is done and modify it to do the plot you are interested in. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Robin Hankin > Sent: Monday, June 18, 2007 6:12 AM > To: R program > Subject: [R] triangle contour plots > > Suppose I have three numbers p1, p2, p3 with 0 <= p1,p2,p3 <= > 1 and p1+p2+p3=1, > and a function f=f(p1,p2,p3) = f(p1,p2,1-p1-p2). > > How to draw a contour plot of f() on the p1+p2+p3=1 plane, > that is, an equilateral triangle? > > Functions triplot(), triangle.plot(), and ternaryplot() give > only scatterplots, AFAICS > > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton European Way, > Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Robin Hankin wrote:> Suppose I have three numbers p1, p2, p3 with > 0 <= p1,p2,p3 <= 1 and p1+p2+p3=1, > and a function f=f(p1,p2,p3) = f(p1,p2,1-p1-p2). > > How to draw a contour plot of f() on the p1+p2+p3=1 plane, > that is, an equilateral triangle? > > Functions triplot(), triangle.plot(), and ternaryplot() give > only scatterplots, AFAICS >Hi Robin, I was all ready to say that triax.plot could do what you want if I added a "type" argument, but found that for some reason both "points" and "lines" are only reading the first color in a vector. If I send more than one color to either function, I only get the first one. If I can sort this out, it's easy to use triax.plot to do what you want (unless, of course, you are satisfied with lines of one color). Jim
Hi, I used similar things to Chemical Mixture Modelling. ( Scheffe model) I make the function below. 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) ) } ######################################################### # Example: f = function(x1,x2){ x3 = 1 - x1 - x2 z = x1 + 0*x2 -x3 + 4*x1*x2 + 27*x1*x2*x3 return( z ) } z = trimage( f ) par( xaxt='n', yaxt='n', bty='n', pty='s') image( z, col=rainbow(256) ) contour( z, add=T ) Robin Hankin wrote:> Suppose I have three numbers p1, p2, p3 with > 0 <= p1,p2,p3 <= 1 and p1+p2+p3=1, > and a function f=f(p1,p2,p3) = f(p1,p2,1-p1-p2). > > How to draw a contour plot of f() on the p1+p2+p3=1 plane, > that is, an equilateral triangle? > > Functions triplot(), triangle.plot(), and ternaryplot() give > only scatterplots, AFAICS >_______________________________________________________ Experimente j? e veja as novidades.