Dear All, I tried to plot contour lines using R function contour, but got the results which are not expected. require(RTOMO) x <- seq(-1,1,0.1) y <- seq(-1,1,0.1) xy <- meshgrid(x,y) z <- xy$x^2+ 3*xy$y^2 contour(x,y,z,col="blue",xlab="x",ylab="y") The above code gave me the contour graph for z=3*x^2+y^2 rather than z=x^2+3*y^2. Is anyone know the reason? Thank you very much in advance. David [[alternative HTML version deleted]]
On 11/08/2010 11:16 AM, ba ba wrote:> Dear All, > > I tried to plot contour lines using R function contour, but got the results > which are not expected. > > require(RTOMO) > x <- seq(-1,1,0.1) > y <- seq(-1,1,0.1) > xy <- meshgrid(x,y) > > z <- xy$x^2+ 3*xy$y^2 > contour(x,y,z,col="blue",xlab="x",ylab="y") > > The above code gave me the contour graph for z=3*x^2+y^2 rather than > z=x^2+3*y^2. Is anyone know the reason? Thank you very much in advance.A problem in RTOMO, or a misuse of it? If I do the calculation of z as z <- outer(x, y, function(x,y) x^2+3*y^2) I get the expected result. Duncan Murdoch
On Aug 11, 2010, at 11:16 AM, ba ba wrote:> Dear All, > > I tried to plot contour lines using R function contour, but got the > results > which are not expected. > > require(RTOMO) > x <- seq(-1,1,0.1) > y <- seq(-1,1,0.1) > xy <- meshgrid(x,y) > > z <- xy$x^2+ 3*xy$y^2 > contour(x,y,z,col="blue",xlab="x",ylab="y") > > The above code gave me the contour graph for z=3*x^2+y^2 rather than > z=x^2+3*y^2. Is anyone know the reason?Because contour was expecting a matrix of z values for z and you gave it a list created by a function you did not understand? > meshgrid function (a, b) { return(list(x = outer(b * 0, a, FUN = "+"), y = outer(b, a * 0, FUN = "+"))) } Instead: Use the base function outer(): > x <- seq(-1,1,0.1) > y <- seq(-1,1,0.1) > xy <- outer(x,y, FUN=function(x,y) x^2+ 3*y^2) > > > contour(x,y,xy,col="blue",xlab="x",ylab="y") -- David Winsemius, MD West Hartford, CT