2010-09-24 123 views
6

你能否給我舉一個例子,說明如何使用rgl在軸x,y和z上繪製3個變量,用不同的顏色繪製第四個變量?R:4D plot,x,y,z,colors

感謝

+0

'光柵:: plot3D'使用'rgl'。我在這裏創建了示例數據的圖像:http://stackoverflow.com/questions/5220283/surface-plot-in-r-similar-to-the-one-from-gnuplot – 2011-03-07 17:01:39

回答

5

可以使用根據一個單獨的函數的persp的組合和顏色。下面是一些示例代碼:

## Create a simple surface f(x,y) = -x^2 - y^2 
## Colour the surface according to x^2 only 
nx = 31; ny = 31 
x = seq(-1, 1, length = nx) 
y = seq(-1, 1, length = ny) 
z = outer(x, y, function(x,y) -x^2 -y^2) 
## Fourth dim 
z_col = outer(x, y, function(x,y) x^2) 

## Average the values at the corner of each facet 
## and scale to a value in [0, 1]. We will use this 
## to select a gray for colouring the facet. 
hgt = 0.25 * (z_col[-nx,-ny] + z_col[-1,-ny] + z_col[-nx,-1] + z_col[-1,-1]) 
hgt = (hgt - min(hgt))/ (max(hgt) - min(hgt)) 

## Plot the surface with the specified facet colours. 
persp(x, y, z, col = gray(1 - hgt)) 
persp(x, y, z, col=cm.colors(32)[floor(31*hgt+1)], theta=-35, phi=10) 

這給:

Sample output

RGL

這是相當簡單使用上述技術與RGL庫:

library(rgl) 
## Generate the data using the above commands 
## New window 
open3d() 

## clear scene: 
clear3d("all") 

## setup env: 
bg3d(color="#887777") 
light3d() 

surface3d(x, y, z, color=cm.colors(32)[floor(31*hgt+1)], alpha=0.5) 
+0

5年太晚評論:'cm.colors(31)[floor(31 * hgt + 1)]'會給出一些越界值(NA),因爲'hgt'可以是1.你可能是指cm。顏色(32)[floor(31 * hgt + 1)]'? – YuppieNetworking 2015-02-27 15:34:10

+0

@YuppieNetworking謝謝並修復。 – csgillespie 2015-02-27 16:02:08

0

看看example(points3d)

r3d幫助頁面顯示如何繪製座標軸。

x <- c(0, 10, 0, 0) 
y <- c(0, 0, 100, 0) 
z <- c(0, 0, 0, 1) 
i <- c(1,2,1,3,1,4) 
labels <- c("Origin", "X", "Y", "Z") 
text3d(x,y,z,labels) 
segments3d(x[i],y[i],z[i]) 

現在,添加一些點

dfr <- data.frame(x = 1:10, y = (1:10)^2, z = runif(10), col = rainbow(10)) 
with(dfr, points3d(x, y, z, col = col)) 
+0

我沒有得到任何結果與您的代碼 – skan 2010-09-25 21:02:27

+0

@ user425895:您應該看到3個標記​​的軸和一些點。檢查你可以用'rgl'顯示*任何*。然後檢查您是否正確複製並粘貼。 – 2010-09-26 10:18:09

2

有一個例子PLOT3D格式,如果你在談論3D空間繪製點和着色他們:

x <- sort(rnorm(1000)) 
y <- rnorm(1000) 
z <- rnorm(1000) + atan2(x,y) 
plot3d(x, y, z, col=rainbow(1000)) 

但如果你的意思是用第四個變量(例如分組變量)對點進行着色,然後我們可以通過創建一個分組變量來修改上面的例子

grp <- gl(5, 200) ## 5 groups 200 members each 
## now select the colours we want 
cols <- 1:5 

## Now plot 
plot3d(x, y, z, col=cols[grp]) 

好的,這是你想要的嗎?

X <- 1:10 
Y <- 1:10 
## Z is now a 100 row object of X,Y combinations 
Z <- expand.grid(X = X, Y = Y) 
## Add in Z1, which is the 3rd variable 
## X,Y,Z1 define the surface, which we colour according to 
## 4th variable Z2 
Z <- within(Z, { 
    Z1 <- 1.2 + (1.4 * X) + (-1.9 * Y) 
    Z2 <- 1.2 + (1.4 * X) - (1.2 * X^2) + (1.9 * Y) + (-1.3 * Y^2) 
    Z3 <- 1.2 + (1.4 * X) + (-1.9 * Y) + (-X^2) + (-Y^2)}) 
## show the data 
head(Z) 
## Set-up the rgl device 
with(Z, plot3d(X, Y, Z1, type = "n")) 
## Need a scale for Z2 to display as colours 
## Here I choose 10 equally spaced colours from a palette 
cols <- heat.colors(10) 
## Break Z2 into 10 equal regions 
cuts <- with(Z, cut(Z2, breaks = 10)) 
## Add in the surface, colouring by Z2 
with(Z, surface3d(1:10,1:10, matrix(Z1, ncol = 10), 
        color = cols[cuts], back = "fill")) 
with(Z, points3d(X, Y, Z1, size = 5)) ## show grid X,Y,Z1 

這裏的一個變型,其中平面表面是Z1彎曲(Z3)。

## Set-up the rgl device plotting Z3, a curved surface 
with(Z, plot3d(X, Y, Z3, type = "n")) 
with(Z, surface3d(1:10,1:10, matrix(Z3, ncol = 10), 
        color = cols[cuts], back = "fill")) 

的我做了什麼讓Z2可能並不重要的細節,但我試圖讓像你鏈接到圖形。

如果我還沒有得到你想要的東西,你可以用一些示例數據編輯你的Q,並給我們一個更好的想法,你想要什麼?

HTH

+0

這樣的事情。 http://addictedtor.free.fr/graphiques/graphcode.php?graph=90但它有點令人困惑 – skan 2010-09-24 10:56:50

+0

@ user425895:我編輯了我的答案,試圖獲得類似於您鏈接的圖表。這種努力更像你想要的嗎? – 2010-09-24 12:32:32

+0

嗨,最後一行不起作用,它說Z2沒有找到。致歉; – skan 2010-09-25 20:53:31

相關問題