我想繪製一個x行和y列的z值矩陣作爲類似於MATLAB的這個圖表的曲面。R中的曲面圖Q-compable to surf()在matlab中
曲面圖:
代碼生成矩陣:
# Parameters
shape<-1.849241
scale<-38.87986
x<-seq(from = -241.440, to = 241.440, by = 0.240)# 2013 length
y<-seq(from = -241.440, to = 241.440, by = 0.240)
matrix_fun<-matrix(data = 0, nrow = length(x), ncol = length(y))
# Generate two dimensional travel distance probability density function
for (i in 1:length(x)) {
for (j in 1:length(y)){
dxy<-sqrt(x[i]^2+y[j]^2)
prob<-1/(scale^(shape)*gamma(shape))*dxy^(shape-1)*exp(-(dxy/scale))
matrix_fun[i,j]<-prob
}}
# Rescale 2-d pdf to sum to 1
a<-sum(matrix_fun)
matrix_scale<-matrix_fun/a
我可以用幾個方法(persp(), persp3d(), surface3d()
)來生成表面圖,但顏色不顯示z值(在矩陣內保持的概率)。 z值似乎只顯示爲高度,而不像MATLAB圖中的差分顏色。的圖形碼和圖形
實施例:最後一個圖形
任何其他提示,以重新創建在R中的圖像將是非常理解的
library(rgl)
persp3d(x=x, y=y, z=matrix_scale, color=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)))
surface3d(x=x, y=y, z=matrix_scale, color=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)))
persp(x=x, y=y, z=matrix_scale, theta=30, phi=30, col=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)), border=NA)
圖像(即圖例條,軸刻度線等)
也許''filled.contour'可能會有幫助嗎? –