0

My specific example: I have a dataframe, say 'df', containing columns of x coordinates, y coordinates, elevation, and I have a vector of precipitation values associated with the x/y coordinates (say the variable name is 'prec').

I can create a 3d representation with:

myColorRamp <- function(colors, values) {
v <- (values - min(values))/diff(range(values))
x <- colorRamp(colors)(v)
rgb(x[,1], x[,2], x[,3], maxColorValue = 255)
}
library(rgl)
cols <- myColorRamp(c("red", "white", "blue"), prec)
plot3d(df$xcoord, df$ycoord, df$Elev, type="p", size=3, xlab='x', ylab='y', zlab='elevation', col = cols)

I'm looking to basically have a 2d representation of the same picture, where instead of a 3d plot, the 3rd dimension of elevation is represented using contour lines.

ie: I want a contour plot like so: http://mathworld.wolfram.com/images/eps-gif/ContourPlot_1000.gif but instead of the colors indicating magnitude of the elevation, I want it to indicate the levels of precipitation.

I've looked at https://stats.stackexchange.com/questions/31726/scatterplot-with-contour-heat-overlay and R: Scatterplot with too many points but they don't seem to apply to this particular problem and I'm struggling with the example given in http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/filled.contour.html because my z value is a vector, not a matrix.

Any help is appreciated.

Community
  • 1
  • 1
swu4
  • 345
  • 3
  • 10
  • 1
    Posting df or a sample of df here e.g. using command head(df) or head(df, 10) and also 'prec' will make it easier for others to help you. – rnso Oct 30 '14 at 04:14
  • What does `my z value is a vector, not a matrix` mean? Can you not create a matrix? That's a very standard way to store 3d data – RockScience Oct 30 '14 at 04:16
  • possible duplicate of [spatial data xyz to matrix](http://stackoverflow.com/questions/15208750/spatial-data-xyz-to-matrix) – RockScience Oct 30 '14 at 04:27
  • possible duplicate of [R: Plotting a 3D surface from x, y, z](http://stackoverflow.com/questions/3979240/r-plotting-a-3d-surface-from-x-y-z) – RockScience Oct 30 '14 at 05:16

1 Answers1

0

You need to convert first your df$xcoord,df$ycoord,df$Elev vectors to a matrix.

Basically create a grid and fill it with the points that you have in your vectors. Some points of the grid will remain NA, but that should work.

See also this existing stackoverflow question
EDIT: all the information is also available in this existing stackoverflow question

Community
  • 1
  • 1
RockScience
  • 17,932
  • 26
  • 89
  • 125