Open jamesa8 opened 9 years ago
Here my 2 cents:
data<-data.frame(x=cumsum(rnorm(100)), y=cumsum(rnorm(100)), z=cumsum(rnorm(100))) data$cluster <- cut(x = 1:length(data$x), breaks = 3)
rgl_plot <- function( data ){ require(rgl) #3d graphic library require(clusterSim) #function data.Normalization
data <- data.frame(cbind(data.Normalization(data[,1:3],"n4",normalization="column"),cluster=data$cluster))
data$colors <-rainbow(length(levels(data$cluster)))[data$cluster]
distance_x<-1.1 #legend x distance from origin distance_y<-1.1 #legend y distance from origin separation_z<-0.1 #vertical separation between legend elements
legend <- data.frame(x=rep(distance_x,length(levels(data$cluster))), y=rep(distance_y,length(levels(data$cluster))), z=seq(0.1, (0.1+separation_z*(length(levels(data$cluster))-1)), separation_z), cluster=levels(data$cluster), colors=rainbow(length(levels(data$cluster))))
size <- ifelse(((100/length(data$x))^(1/3))_0.015>0.01, ((100/length(data$x))^(1/3))_0.015, 0.01) #the size of the spheres depending on the number of your observations
rgl.bg(col="white", fogtype="linear")
rgl.spheres(data$x, data$y, data$z, color=data$colors, radius=size)
rgl.spheres(legend$x, legend$y, legend$z, color=legend$colors, radius=size) text3d(legend$x, legend$y, legend$z, texts=legend$cluster, adj=1.2, cex=0.4, col="black")
a<-list(c(0.1,0.1),c(0.2,0.2),c(0.3,0.3),c(0.4,0.4),c(0.5,0.5),c(0.6,0.6),c(0.7,0.7),c(0.8,0.8),c(0.9,0.9)) b<-list(c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1),c(-0.1,1)) c<-list(c(0,0),c(0,0),c(0,0),c(0,0),c(0,0),c(0,0),c(0,0),c(0,0),c(0,0))
rgl.lines(c(-0.2,1.1), c(0,0), c(0,0), color="black") rgl.triangles(c(1.05,1,1), c(0,0.02,-0.02), c(0,0,0), color="black") for(i in 1:9){ rgl.lines(a[[i]],b[[i]],c[[i]], color="black", alpha=0.3)#x,y rgl.lines(a[[i]],c[[i]],b[[i]], color="black", alpha=0.1)#x,z } rgl.texts(0.97, 0, 0.01, "X", adj=1, cex=0.5, color="black")
rgl.lines(c(0,0), c(-0.2,1.1), c(0,0), color="black") rgl.triangles(c(0,0.02,-0.02), c(1.05,1,1), c(0,0,0), color="black") for(i in 1:9){ rgl.lines(b[[i]],a[[i]],c[[i]], color="black", alpha=0.3)#x,y rgl.lines(b[[i]],c[[i]],a[[i]], color="black", alpha=0.1)#x,z } rgl.texts(0, 0.97, 0.01, "Y", adj=1, cex=0.5, color="black")
rgl.lines(c(0,0), c(0,0), c(-0.2,1.1), color="black") rgl.triangles(c(0,0.02,-0.02), c(0,0,0), c(1.05,1,1),color="black") for(i in 1:9){ rgl.lines(c[[i]],a[[i]],b[[i]], color="black", alpha=0.1)#y,z rgl.lines(c[[i]],b[[i]],a[[i]], color="black", alpha=0.1)#y,z } rgl.texts(0.01, 0.01, 0.97, "Z", adj=1, cex=0.5, color="black")
rgl.planes(c(0,0,-1), color="black", alpha=0.1) }
output$myWebGL <- renderWebGL({ rgl_plot(data) })
Saluti, Marco
Thanks Marco. This is very helpful and is a useful workaround. :+1: :)
Best, Amber
I am attempting to add a legend to my WebGL scene in shiny. Although the code works locally with the legend displayed, this does not work when it is ran on shiny. I am just attempting to run some example code at this point.
When it is ran as a regular script with open3d() par3d(windowRect = c(100, 100, 612, 612))
The window renders properly with the legend. I am not quite sure why this happens and have yet to figure out a workaround for this issue. If you have any ideas for a temporary workaround it would be appreciated.
Thanks.