[英]Adding a legend to scatter3d plot
交互式 3D 繪圖的可能軟件包之一是 rgl。 我想做的是根據一些因子變量構建帶有顏色編碼的 3D 散點圖。 3D 維散點圖用於 plsr 分析的結果載荷。
結果圖看起來像
示例數據在表中給出:
> 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)
獲得的情節具有相同的風格,但更簡單,因為變量“Class”只有兩個級別:'global'和'local'
問題是:是否有可能在 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)
根據類別帶有標簽的圖:
這不是plot3d
圖像(除非您可能加載了另一個包),但看起來像是使用 John Fox 的car
包中的scatter3d
函數構建的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)
使用新數據,這是為了響應對文本和點的請求:
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)
我使用公式方法來創建組,並通過將它們變成白色來“刪除”組標簽,因為我不知道如何以更簡單的方式做到這一點。 然后我使用legend3d
創建了一個圖例......這非常簡單。 只要確保點和圖例顏色匹配。 也許這很笨重,但它做了我需要它做的事情!
scatter3d(Biomass~Salinity*Moisture|Competitor, data=phrag19, labels=FALSE,
surface=FALSE,
grid = FALSE,
ellipsoid = FALSE,
axis.col = c("black", "black", "black"),
text.col=c("white","white","white","white","white","white","white"),
surface.col=c("#1D1B1A","#C70039",
"#94CC00","#A5D7F3","#D2A7DC","#1CAD7A","#FFB114"))
legend3d("right", legend = levels(phrag19$Competitor),
col = c("#1D1B1A","#C70039", "#94CC00","#A5D7F3","#D2A7DC","#1CAD7A","#FFB114"),
pch = 18)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.