Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

r - Adding a legend to an rgl 3d plot

I am trying to draw a 3d plot using rgl with colors legend indicating which color refer to which class (called 'cut.rank'):

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))
) 

But I can't seem to get the legend showing up in the plot. (See attached plot) Any idea ?enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure the colkey option applies to the plot3d function. You can use legend3d instead to add a legend the way you would in normal 2d plots:

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

Update: colkey is an argument to scatter3D in the plot3D package (not the same as the plot3d function in the rgl package). You can use this as well:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...