Question

(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 !

Was it helpful?

Solution

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 !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top