2010-09-24 39 views
6

क्या आप मुझे एक उदाहरण दे सकते हैं कि अक्षरों x, y और z पर 3 चरों को प्लॉट करने के लिए आरजीएल का उपयोग कैसे करें और चौथे रंग के विभिन्न रंगों के साथ?आर: 4 डी प्लॉट, एक्स, वाई, जेड, रंग

धन्यवाद

+0

'रेखापुंज :: plot3D' 'rgl' का उपयोग करता है। मैंने यहां उदाहरण डेटा की छवि बनाई है: http://stackoverflow.com/questions/5220283/surface-plot-in-r-similar-to-the-one-from-gnuplot –

उत्तर

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) [मंजिल (31 * एचजीटी + 1)] 'कुछ सीमाओं (एनए) से कुछ दे देंगे क्योंकि' hgt' हो सकता है 1. आप शायद 'सेमी का मतलब है। रंग (32) [मंजिल (31 * एचजीटी + 1)] '? – YuppieNetworking

+0

@YuppieNetworking धन्यवाद और तय। – csgillespie

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

+0

@ user425895: आपको 3 लेबल वाले अक्ष और कुछ बिंदु देखना चाहिए। जांचें कि आप 'rgl' के साथ * कुछ भी * प्रदर्शित कर सकते हैं। फिर जांचें कि आपने सही ढंग से प्रतिलिपि बनाई है और चिपकाया है। –

2

एक उदाहरण है जोड़ने plot3d यदि आप एक 3 डी अंतरिक्ष में स्थानों की प्लॉटिंग और उन्हें रंग के बारे में बात कर रहे हैं?

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

लेकिन अगर समूहबद्ध चर कहते हैं, तो आप 4 वें चर द्वारा बिंदुओं को रंगना चाहते हैं, फिर हम ग्रुपिंग चर

बनाकर ऐसा करने के लिए ऊपर दिए गए उदाहरण को संशोधित कर सकते हैं
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 शायद पाने के लिए कोई फर्क नहीं पड़ता किया विस्तार, लेकिन मैं ग्राफ आप से जुड़ा हुआ की तरह कुछ पाने की कोशिश की।

यदि मुझे अभी भी जो चाहिए वह नहीं मिला है, तो क्या आप कुछ उदाहरण डेटा के साथ अपना क्यू संपादित कर सकते हैं और हमें क्या चाहते हैं इसका एक बेहतर विचार दे सकते हैं?

HTH

+0

इस तरह कुछ। http://addictedtor.free.fr/graphiques/graphcode.php?graph=90 लेकिन यह थोड़ा उलझन में है – skan

+0

@ user425895: मैंने आपके द्वारा संपादित किए गए ग्राफ की तरह कुछ प्राप्त करने का प्रयास करने के लिए अपना उत्तर संपादित कर दिया है।क्या यह प्रयास आप चाहते हैं की तरह अधिक है? –

+0

हाय, अंतिम पंक्ति काम नहीं करती है, यह कहता है कि Z2 नहीं मिला। – skan

संबंधित मुद्दे