0

I have a dataset countnp with 3 columns as below:

>countnp
Time  Sections  Count
   0         0    370
   1         0      9
   1         1    127
   1         2    119
   1         3     55
   1         4     34
   1         5     55

and so on..The values of Time and Sections range from 0 to 7. Now, I want to create a contour plot of Time vs Sections, but I'm getting an error saying - increasing 'x' and 'y' values expected. Here Time and Sections both cannot be sorted at the same time. Please help me with this. The code that I'm using:

>nplevel <- seq(0, 20, 2)
>contour(countnp$Time, countnp$Sections, countnp$Count, levels=nplevel)
Usha
  • 21
  • 2
  • 1
    google my friend, first entry http://stackoverflow.com/questions/5847042/how-to-draw-a-contour-plot-when-data-are-not-on-a-regular-grid – MLavoie Dec 15 '15 at 16:39
  • I know how to sort the data. I would have done it if the three variables were independent, but they are not! I wanted to know how to make this work if the 3 of them are in a data set. – Usha Dec 15 '15 at 17:29

1 Answers1

1

The contour() function is expecting a matrix, z, as you can see in the help file or following the link that MLavoie provided.

Alternatively, you could use the contourplot() function from the lattice package, instead.

countnp <- data.frame(
  Time = c(0, 1, 1, 1, 1, 1, 1), 
  Sections = c(0, 0, 1, 2, 3, 4, 5), 
  Count = c(370, 9, 127, 119, 55, 34, 55))

library(lattice)
contourplot(Count ~ Time + Sections, data=countnp)
Jean V. Adams
  • 4,634
  • 2
  • 29
  • 46