3

(Sorry if my english is not perfect... :-s)

My question is almost the same that in this subject : Creating a filled contour plot using data in lists

But, the difference is that I would like to plot the density corresponding to my (x,y) coordinates, in only 1 direction ! I explain : I have a data.frame like this :

X <- matrix(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), nrow=4)
Y <- matrix(c(-1.0190, -0.9617, -0.9044, -0.8470, -1.0617, -0.9641, -0.8664, -0.7688,  0.4623, 0.6012,  0.7401,  0.8790), nrow=4)
Z <- matrix(c(3.9216,  3.9216,  3.9216, 11.7647,  1.9608,  1.9608,  1.9608, 11.7647,  1.9608, 1.9608,  9.8039,  9.8039), nrow=4)
Niveau <- data.frame(X=c(X),Y=c(Y),Z=c(Z))

X representes the x-coordinates, Y the y-coordinates and Z the density in percentage in the Y direction only. For each x-coordinate, I calculated density in the Y direction. And the result is in the Z vector. You can see that it is not a regular grid in (x,y). And I would like to plot a "contour plot" of the density Z, but so far, I didn't success... I tried this command :

ggplot(Niveau, aes(x=X, y=Y, z=Z)) + geom_density2d()

But it plots the density in the x AND y direction, and I want only in the Y direction. When I tried the command :

filled.contour(t(Z), nlevels=10)

It doesn't respect the (x,y) coordinates. And it is impossible to implement :

filled.contour(X,Y,Z, nlevels=10)

After, some research, i find this subject, and my probleme is the same : How to draw a contour plot when data are not on a regular grid?

But the answer doesn't correspond : the solution is "regular grid" in (x,y), and that's not what I have ! Somebody can help ?

Thanks !

Community
  • 1
  • 1
sandikou
  • 133
  • 1
  • 11
  • 1
    You can adapt this answer: http://stackoverflow.com/questions/8508059/simple-r-3d-interpolation-surface-plot/8508463#8508463 – Ben Bolker Aug 30 '13 at 12:55
  • Thanks !!! It works !! From the time I was looking for... Here's what I did : my.heat.colors <- function(x) { rev(heat.colors(x, alpha=1)) } my.matrix <- interp(X,Y,Z, length=500) ind.mat.na <- which(is.na(c(my.matrix$z))) my.matrix$z[ind.mat.na] <- 0 filled.contour(my.matrix, nlevels=10, color=my.heat.colors) And now I will plot contours on this. Thank you again ! – sandikou Aug 30 '13 at 13:32
  • Congratulations for solving your own problem (with a hint). It would be good form, and will enhance your StackOverflow reputation, if you go ahead and post your solution as an answer (you may have to wait a few hours -- I'm not sure). – Ben Bolker Aug 30 '13 at 13:38
  • I'll post it tomorrow :-) – sandikou Aug 30 '13 at 13:55

1 Answers1

3

Thanks !!! It works !! From the time I was looking for...

Here's what I did :

 library(akima)
 my.heat.colors <- function(x) { rev(heat.colors(x, alpha=1)) }
 my.matrix  <- interp(X,Y,Z)
 ind.mat.na <- which(is.na(c(my.matrix$z)))
 my.matrix$z[ind.mat.na] <- 0
 filled.contour(my.matrix, nlevels=10, color=my.heat.colors)

And now I will plot contours on this.

Thank you again !

Michael
  • 13,244
  • 23
  • 67
  • 115
sandikou
  • 133
  • 1
  • 11