2013-09-27 4 views
5

대화 형 3D 플롯에 사용할 수있는 패키지 중 하나는 rgl입니다. 내가 뭘하고 싶은지는 어떤 요인 변수에 따라 색상 코딩으로 3D scatterplot을 만드는 것입니다. 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를 가지고있다. 나 스타일하지만, 변수 "클래스"의 두 가지 수준이 있기 때문에 훨씬 간단합니다 : 가 RGL 내 전설을 추가 할 가능성이 있습니다 :

enter image description here

질문는 '글로벌'과 '지역' 아니면 어떤 독립적 인 전설이 줄거리에 붙어있을 수 있습니까? 미리 도움을 청하십시오!

답은 클래스에 따라 라벨

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) 

줄거리하십시오 plot3d 이미지 아니다 enter image description here

+0

코드를 추가하십시오 귀하의 질문에 대한 (실제 또는 만들어 낸 하나) 가능한 데이터는 [최소에 repr를 만들 수 있도록하는 경우 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

(아마 당신은 다른 패키지로드를하지 않는 한),하지만 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