2015-01-15 10 views
6

Sto cercando di tracciare un grafico 3D usando rgl con colori leggenda che indica quale colore si riferiscono a quale classe (chiamato 'cut.rank'):L'aggiunta di una leggenda a un rgl grafico 3D

plot3d(
data.focus$normalized.price_shipping, 
data.focus$seller_feedback_score_rank, 
data.focus$seller_positive_feedback_percent_rank, 
col=as.factor(data.focus$cut.rank), 
size=1, 
type='s', 
xlab = 'Normalized Price', 
ylab = 'Seller Feedbacl Score Rank', 
zlab = 'Seller Positive Feedback Percent Rank', 
main = 'Rank By Price, Feedback score and Positive Feedback Score', 
sub = 'Search Rank has 3 colored levels', 
colkey = list(length = 0.5, width = 0.5, cex.clab = 0.75)) 
) 

Ma non posso sembra che la leggenda sembri nella trama. (Vedi trama allegata) Qualche idea? enter image description here

risposta

10

Non sono sicuro che l'opzione colkey si applica alla funzione plot3d. È possibile utilizzare legend3d invece per aggiungere una leggenda il modo in cui si farebbe in normali grafici 2D:

library(rgl) 

#dummy data 
set.seed(1) 
x <- cumsum(rnorm(100)) 
y <- cumsum(rnorm(100)) 
z <- cumsum(rnorm(100)) 
cuts = cut(x = 1:length(x), breaks = 3) 

# open 3d window 
open3d() 

# resize window 
par3d(windowRect = c(100, 100, 612, 612)) 

# plot points 
plot3d(x, y, z, 
     col=rainbow(3)[cuts], 
     size = 2, type='s') 

# add legend 
legend3d("topright", legend = paste('Type', c('A', 'B', 'C')), pch = 16, col = rainbow(3), cex=1, inset=c(0.02)) 

# capture snapshot 
snapshot3d(filename = '3dplot.png', fmt = 'png') 

enter image description here

Aggiornamento: colkey è un argomento per scatter3D nel pacchetto plot3D (non è la stessa della funzione plot3d nel pacchetto rgl). È possibile utilizzare anche questo:

library(plot3D) 
scatter3D(x,y,z, col = rainbow(3)[cuts], colvar = NA, colkey = F, pch = 16) 
legend("topright", paste('Type', c("A", "B", "C")), pch = 16, col = rainbow(3), cex=1, inset=c(0.02,0.2)) 

enter image description here

Problemi correlati