2013-09-27 58 views
5

交互式3D圖的可能包之一是rgl。我想要做的是根據一些因子變量,用顏色編碼構建三維散點圖。 3D維散點圖用於從plsr分析得出的加載結果。將圖例添加到scatter3d圖

所得情節看起來像

3D scatterplot of PLS loadings with color coding according to the groups of variables

示例數據在表中給出:

> loadings 

     |  Comp 1   |  Comp 2   | Comp 3   | Class 
-------------------------------------------------------------------------------------------      
TEMP | -0.0607044182964255 | "0.0437618450165671"  |"0.045124991801441" | "global" 
MW | "-0.13414890573833" | "-0.0970537799069731" |0.263043734662182" | "local" 
DM |"-0.183751529577861" | "-0.102703237685933" |"0.0640549385564205" | "global" 
CHG |"-0.0558781715833019"| "0.125155347350922"  |"-0.119258450107321" | "local" 

或可以產生:

loadings <- data.frame(Comp1 = c(1.2, 3.4, 5.6, 13.1), Comp2 = c(4.3, 1.2, 7.7, 9.8), 
         Comp3 = c(1.2,6.9,15.6,15.0), 
         row.names = c("TEMP", "MW", "DM", "CHG"), 
         Class = c("global", "local", "global", "local")) 
scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
      point.col = as.numeric(as.factor(loadings[,4])), size = 10) 

獲得情節有sa我的風格,但要簡單得多,因爲只有兩個級別變量「類」:「全局」和「本地」

enter image description here

問題是: 這是任何可能性RGL中添加傳說或者一些獨立的傳說可以附加到情節? 在此先感謝您的幫助!

答案是:

scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
      point.col = as.numeric(as.factor(loadings[,4])), size = 10, type = 's') 
text3d(x=1.1, y=c(.9,1), z=1.1,levels(loadings[[4]]),col="black") 
points3d(x=1.2,y=c(.9,1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5) 

的帶標籤根據類的情節: enter image description here

+0

請添加代碼,如果可能的數據(真實或虛構的)你的問題,使一個[最低再版oducible示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – SlowLearner

+0

新代碼會引發錯誤:「complete.cases(x,y,z)中的錯誤:...」。 P –

+0

修正了錯誤。 – Boddha

回答

1

這不是一個plot3d圖像(除非你也許有另一個包加載),但看起來像一個scatter3d由John Fox的car包構成scatter3d功能:

require(rgl) 
    require(car) 
    scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
      point.col = as.numeric(as.factor(loadings[,4])), size = 10) 

scatter3d函數依賴於rgl函數,但有很多自定義選項。你沒有提供的代碼來構建一個「傳奇」,所以我在RGL :: text3d提供的例子發揮各地:

text3d(1+ font/8, 1+cex/4, 1+famnum/8, text=paste(family, font), adj = 0.5, 
     color="blue", family=family, font=font, cex=cex) 

enter image description here

隨着新的數據,這是響應於文本請求並指出:

scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
      point.col = as.numeric(as.factor(loadings[,4])), size = 10) 
text3d(x=1.1, y=c(.9,1,1.1), z=1.1, names(loadings) ,col="black") 
points3d(x=1.2,y=c(.9,1,1.1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5) 

enter image description here

+0

是的,確實是scatter3d,修正了它。那麼,它看起來不錯,但很奇怪,並沒有很多信息。檢查文字。並且我需要在圖例中具有與數據點的顏色標籤對應的顏色標籤。 – Boddha

+0

如展示的那樣,以結構化方式繪製點和文本應該不會太困難。您需要詳細描述所需的內容。 –

+0

現在是它,我會把結果放在上面。謝謝! – Boddha