2013-08-30 23 views
3

(很抱歉,如果我的英文不是很好,...:-s)如何繪製3個變量的R A data.frame一個filled.contour圖(無規則的網格)?

我的問題幾乎是一樣的,在這個問題: Creating a filled contour plot using data in lists

但是,不同的是,我想繪製密度僅對應於my(x,y)座標,只有1個方向! 予解釋: 我有data.frame這樣的:

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的x座標,Y的y座標和Z的密度在僅在Y方向上的百分比。對於每個x座標,我計算了Y方向的密度。結果是在Z向量中。 您可以看到它不是(x,y)中的規則網格。 我想畫出密度Z的「等高線圖」,但到目前爲止,我沒有成功... 我想這個命令:

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

但它繪製在x密度和y方向,我只想在Y方向。 當我試圖命令:

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

它不尊重(X,Y)座標。它是不可能實現:

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

之後,一些研究,我發現這個問題上,我probleme是一樣的: How to draw a contour plot when data are not on a regular grid?

但答案不對應:該解決方案是「常規網格「(x,y),這不是我擁有的! 有人可以幫忙嗎?

謝謝!

+1

你可以適應這個答案:http://stackoverflow.com/questions/8508059/simple-r-3d-interpolation-surface-plot/8508463#8508463 –

+0

謝謝!有用 !!從時間我一直在尋找... 這裏就是我所做的: my.heat.colors < - 函數(X){轉(heat.colors(X,阿爾法= 1))} my.matrix < - interp的(X,Y,Z,長度= 500) ind.mat.na < - 其中(is.na(C(my.matrix $ Z))) my.matrix $ Z [ind.mat.na ] < - 0 filled.contour(my.matrix,NLEVELS = 10,顏色= my.heat.colors) 現在我將繪製輪廓在此。 再次感謝您! – sandikou

+0

祝賀你解決你自己的問題(帶提示)。如果你繼續發佈你的解決方案作爲答案(你可能需要等待幾個小時 - 我不確定),這將是一種很好的形式,並且會增強你的StackOverflow聲譽。 –

回答

3

謝謝!有用 !!從時間我一直在尋找...

這裏就是我所做的:

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) 

現在我將繪製在這個輪廓。

再次感謝您!