4

I have 3 dimensions that I want to plot, and I want the third dimension to be color.

This will be in R by the way. For instance, my data looks like this

x = [1,2,3,4,1,5,6,3,4,7,8,9]
y = [45,32,67,32,32,47,89,91,10,12,43,27]
z = [1,2,3,4,5,6,7,8,9,10,11,12]

I am trying to use filled.contour, but it giving me an error saying x and y must be in increasing order. But I'm not really sure how to arrange my data such that that is true. Because if I arrange x in increasing order, then y will not be in increasing order.

It is also feasible for me to do a non-filled contour method where it is just data points that are colored. How can I do this in R. Any suggested packages? Please use specific examples. Thanks!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • Is your data really three vectors? Because if you actually read the documentation for `filled.contour` you'll quickly see that that function expects two vectors and a matrix. – joran Sep 24 '11 at 02:37
  • I did read the documentation and I see that it says the values must be ordered. what is the matrix for? can you give me an example. I couldn't find a nice example anywhere – CodeGuy Sep 24 '11 at 02:52

1 Answers1

5
jet.colors <-
   colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                      "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
plot(x,y, col=jet.colors(12)[z], ylim=c(0,100), pch=20, cex=2)
legend(8.5,90, col = jet.colors(12)[z], legend=z, pch=15)

And if you want to check the coloring you can label the points with the z value:

text(x, y+2, labels=z)  #offset vertically to see the colors

enter image description here

Another option is to use package:akima which does interpolations on irregular (non)-grids:

require(akima)
require(lattice)
ak.interp <- interp(x,y,z)
pdf(file="out.pdf")
levelplot(ak.interp$z, main="Output of akima plotted with lattice::levelplot", contour=TRUE)
 dev.off()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • a subsidiary question: how can I get a (continuous) legend similar to the second plot in your answer, but for the first plot? – Mortimer Nov 30 '11 at 12:41