问题
Hi guys,
I'm using rgl and shinyrgl to produce a 3D PCA plot within my Shiny app. I'm then using the legend3d
function to create the legend. As you can see from the image above, the legend is significantly lower resolution than the plot. Has anyone else experienced this problem?
I've included the code being used to draw this below. Since it's Shiny it is a little difficult to produce a full working example. I'm more looking to see if anyone else has fixed this problem.
Matt
UI
tabItem(tabName = "3DTab",
fluidRow(
column(6,
rglwidgetOutput(outputId = "TDPCA")),
column(6,
box(title = "Customisation",
width = NULL,
collapsible = TRUE,
selectizeInput(inputId = "TDPCAClass",
label = "Class",
choices = c("Day"))))
Server
output$TDPCA <- renderRglwidget({
plot3d(x = TDPCA()$PC1, y = TDPCA()$PC2, z = TDPCA()$PC3, col = TDPCA()[[input$TDPCAClass]],
xlab = paste("PC1", "-", TDPCA.var()["PC1"], "%"), ylab = paste("PC2", "-", TDPCA.var()["PC2"], "%"), zlab = paste("PC3", "-", TDPCA.var()["PC3"], "%"), type = "p")
legend3d("topright",
legend = c("Monday", "Tuesday", "Wednesday", "Thursday", "Saturday"),
pch = 16,
col = palette("default"),
cex=0.65, inset=c(0))
rglwidget()
})
回答1:
Yes, that issue is mentioned in the help page for legend3d
. The problem is that the legend is generated for the current rgl window size, which may be resized later. You want to generate it after resizing.
I'm not quite sure if you can query Shiny about what size the rgl window will be rendered at, but one possibility is just to guess, e.g.
output$TDPCA <- renderRglwidget({
plot3d(x = TDPCA()$PC1, y = TDPCA()$PC2, z = TDPCA()$PC3, col = TDPCA()[[input$TDPCAClass]],
xlab = paste("PC1", "-", TDPCA.var()["PC1"], "%"), ylab = paste("PC2", "-", TDPCA.var()["PC2"], "%"), zlab = paste("PC3", "-", TDPCA.var()["PC3"], "%"), type = "p")
par3d(windowRect = c(0, 0, 512, 512))
legend3d("topright",
legend = c("Monday", "Tuesday", "Wednesday", "Thursday", "Saturday"),
pch = 16,
col = palette("default"),
cex=0.65, inset=c(0))
rglwidget()
})
Edited to add: This page https://groups.google.com/forum/#!topic/shiny-discuss/ZmuZbQfJstg talks about setting the size dynamically.
来源:https://stackoverflow.com/questions/48562604/rgl-low-resolution-legend