[R] Puzzled with contour()

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 26 16:39:20 CEST 2006


I think it would be helpful if this were added to the contour help file.

On 6/26/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> On 6/25/2006 9:33 AM, Ajay Narottam Shah wrote:
> > Folks,
> >
> > The contour() function wants x and y to be in increasing order. I have
> > a situation where I have a grid in x and y, and associated z values,
> > which looks like this:
>
> contour() wants vectors of x and y values, and a matrix of z values,
> where the x values correspond to the rows of z, and the y values to the
> columns.  You have a collection of points which need to be turned into
> such a grid.
>
> There's an interp function in the akima package that can do this in
> general.  In your case, it's probably sufficient to do something like this:
>
> zmat <- matrix(NA, 3, 19)
> zmat[cbind(20*x + 1, y/10 - 1)] <- z
> x <- (0:2)/20
> y <- (2:20)*10
> contour(x,y,zmat)
>
> Duncan Murdoch
>
>
> >
> >               x   y     z
> >       [1,] 0.00  20 1.000
> >       [2,] 0.00  30 1.000
> >       [3,] 0.00  40 1.000
> >       [4,] 0.00  50 1.000
> >       [5,] 0.00  60 1.000
> >       [6,] 0.00  70 1.000
> >       [7,] 0.00  80 0.000
> >       [8,] 0.00  90 0.000
> >       [9,] 0.00 100 0.000
> >      [10,] 0.00 110 0.000
> >      [11,] 0.00 120 0.000
> >      [12,] 0.00 130 0.000
> >      [13,] 0.00 140 0.000
> >      [14,] 0.00 150 0.000
> >      [15,] 0.00 160 0.000
> >      [16,] 0.00 170 0.000
> >      [17,] 0.00 180 0.000
> >      [18,] 0.00 190 0.000
> >      [19,] 0.00 200 0.000
> >      [20,] 0.05  20 1.000
> >      [21,] 0.05  30 1.000
> >      [22,] 0.05  40 1.000
> >      [23,] 0.05  50 1.000
> >      [24,] 0.05  60 0.998
> >      [25,] 0.05  70 0.124
> >      [26,] 0.05  80 0.000
> >      [27,] 0.05  90 0.000
> >      [28,] 0.05 100 0.000
> >      [29,] 0.05 110 0.000
> >      [30,] 0.05 120 0.000
> >      [31,] 0.05 130 0.000
> >      [32,] 0.05 140 0.000
> >      [33,] 0.05 150 0.000
> >      [34,] 0.05 160 0.000
> >      [35,] 0.05 170 0.000
> >      [36,] 0.05 180 0.000
> >      [37,] 0.05 190 0.000
> >      [38,] 0.05 200 0.000
> >      [39,] 0.10  20 1.000
> >      [40,] 0.10  30 1.000
> >
> > This looks like a nice case where both x and y are in increasing
> > order. But contour() gets unhappy saying that he wants x and y in
> > increasing order.
> >
> > Gnuplot generates pretty 3d pictures from such data, where you are
> > standing above a surface and looking down at it. How does one do that
> > in R?
> >
> > Any help will be most appreciated. A dput() of my data object is :
> >
> > structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
> > 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.1, 0.1,
> > 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
> > 0.1, 0.1, 0.1, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
> > 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
> > 0.15, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2,
> > 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.25, 0.25, 0.25, 0.25,
> > 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
> > 0.25, 0.25, 0.25, 0.25, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,
> > 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.35,
> > 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
> > 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.4, 0.4, 0.4, 0.4,
> > 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4,
> > 0.4, 0.4, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45,
> > 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.5,
> > 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
> > 0.5, 0.5, 0.5, 0.5, 0.5, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55,
> > 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55,
> > 0.55, 0.55, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
> > 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.65, 0.65, 0.65,
> > 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65,
> > 0.65, 0.65, 0.65, 0.65, 0.65, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7,
> > 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.75,
> > 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75,
> > 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.8, 0.8, 0.8, 0.8,
> > 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8,
> > 0.8, 0.8, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
> > 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.9,
> > 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9,
> > 0.9, 0.9, 0.9, 0.9, 0.9, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
> > 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
> > 0.95, 0.95, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> > 1, 1, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140,
> > 150, 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80, 90,
> > 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 20, 30,
> > 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170,
> > 180, 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
> > 130, 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70,
> > 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
> > 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150,
> > 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100,
> > 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 20, 30, 40,
> > 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180,
> > 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130,
> > 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80,
> > 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 20,
> > 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
> > 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110,
> > 120, 130, 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50,
> > 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180,
> > 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130,
> > 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80,
> > 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 20,
> > 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
> > 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110,
> > 120, 130, 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50,
> > 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180,
> > 190, 200, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130,
> > 140, 150, 160, 170, 180, 190, 200, 20, 30, 40, 50, 60, 70, 80,
> > 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 20,
> > 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
> > 170, 180, 190, 200, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 1, 1, 1, 1, 0.998, 0.124, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 1, 1, 1, 0.998, 0.71, 0.068, 0, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0.998, 0.898, 0.396, 0.058, 0.002,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0.998, 0.97, 0.726, 0.268,
> > 0.056, 0.006, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0.996, 0.88,
> > 0.546, 0.208, 0.054, 0.012, 0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0.998, 0.964, 0.776, 0.418, 0.18, 0.054, 0.014, 0.002,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.998, 0.906, 0.664, 0.342,
> > 0.166, 0.056, 0.018, 0.006, 0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0, 0.986, 0.862, 0.568, 0.29, 0.15, 0.056, 0.022, 0.008, 0.002,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.954, 0.778, 0.494, 0.26, 0.148,
> > 0.056, 0.024, 0.012, 0.004, 0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> > 0.906, 0.712, 0.43, 0.242, 0.144, 0.058, 0.028, 0.012, 0.006,
> > 0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.878, 0.642, 0.38, 0.222,
> > 0.142, 0.066, 0.034, 0.014, 0.008, 0.004, 0.002, 0, 0, 0, 0,
> > 0, 0, 0, 0, 0.846, 0.586, 0.348, 0.208, 0.136, 0.068, 0.034,
> > 0.016, 0.012, 0.006, 0.004, 0.002, 0, 0, 0, 0, 0, 0, 0, 0.8,
> > 0.538, 0.318, 0.204, 0.136, 0.07, 0.046, 0.024, 0.012, 0.008,
> > 0.004, 0.002, 0.002, 0, 0, 0, 0, 0, 0, 0.762, 0.496, 0.294, 0.2,
> > 0.138, 0.072, 0.05, 0.024, 0.014, 0.012, 0.006, 0.004, 0.002,
> > 0.002, 0, 0, 0, 0, 0, 0.704, 0.472, 0.286, 0.198, 0.138, 0.074,
> > 0.054, 0.028, 0.016, 0.012, 0.008, 0.006, 0.004, 0.002, 0.002,
> > 0, 0, 0, 0, 0.668, 0.438, 0.276, 0.196, 0.138, 0.078, 0.054,
> > 0.032, 0.024, 0.014, 0.012, 0.008, 0.004, 0.004, 0.002, 0.002,
> > 0, 0, 0, 0.634, 0.412, 0.27, 0.194, 0.14, 0.086, 0.056, 0.032,
> > 0.024, 0.016, 0.012, 0.01, 0.006, 0.004, 0.004, 0.002, 0.002,
> > 0, 0, 0.604, 0.388, 0.26, 0.19, 0.144, 0.088, 0.058, 0.048, 0.026,
> > 0.022, 0.014, 0.012, 0.008, 0.006, 0.004, 0.004, 0.002, 0.002,
> > 0, 0.586, 0.376, 0.256, 0.19, 0.146, 0.094, 0.062, 0.052, 0.028,
> > 0.024, 0.014, 0.012, 0.012, 0.008, 0.004, 0.004, 0.004, 0.002,
> > 0.002, 0.566, 0.364, 0.254, 0.192, 0.148, 0.098, 0.064, 0.054,
> > 0.032, 0.024, 0.022, 0.014, 0.012, 0.012, 0.008, 0.004, 0.004,
> > 0.004, 0.002), .Dim = c(399, 3), .Dimnames = list(NULL, c("x",
> > "y", "z")))
> >
> > --
> > Ajay Shah                                      http://www.mayin.org/ajayshah
> > ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
> > <*(:-? - wizard who doesn't know the answer.
> >
> > ______________________________________________
> > 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
>
> ______________________________________________
> 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
>



More information about the R-help mailing list