我想改变下图中点击点的形状和大小。如何实现它?对于这个玩具情节,我已经将原始100k的点数减少到了2k。因此,预期解决方案应该具有高度可扩展性,并且不会偏离原始图,即点击点更新前后的所有颜色应该相同。Shiny:如何更改点击点的形状和/或大小?
library(shiny)
library(plotly)
df <- data.frame(X=runif(2000,0,2), Y=runif(2000,0,20),
Type=c(rep(c('Type1','Type2'),600),
rep(c('Type3','Type4'),400)),
Val=sample(LETTERS,2000,replace=TRUE))
# table(df$Type, df$Val)
ui <- fluidPage(
title = 'Select experiment',
sidebarLayout(
sidebarPanel(
checkboxGroupInput("SelType", "Select Types to plot:",
choices = unique(df$Type),
selected = NA)
),
mainPanel(
plotlyOutput("plot", width = "400px"),
verbatimTextOutput("click")
)
)
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
if(length(input$SelType) != 0){
df <- subset(df, Type %in% input$SelType)
p <- ggplot(df, aes(X, Y, col = as.factor(Val))) +
geom_point()
}else{
p <- ggplot(df, aes(X, Y, col = as.factor(Val))) +
geom_point()
}
ggplotly(p) %>% layout(height = 800, width = 800)
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
else cat("Selected point associated with value: ", d$Val)
})
}
shinyApp(ui, server)
一个相关question这里已经问过,但强调有颜色的点的这种做法不工作(当一个变量的水平数为高,难以对可能已经存在于该图中的颜色进行硬编码)。
同样优异的溶液(1)最大。 – Prradep
将解决方案合并到发布的场景中后,我注意到了一个奇怪的行为(我不确定它是否是预期的)。当没有选择任何“类型”并且点击某个点时,点击的点会以相同的颜色增加大小。虽然形状没有改变,但它具有可扩展性并与其他附近的点明显区分开来。现在,当您选择一个“类型”时,会在同一区域生成一个新图。令人惊讶的是,点击点时上述效果不可用。我认为应该有一点调整来解决这个问题。 – Prradep
另一个观察结果是,如果我们取消选择所选类型并返回到原始绘图,上述功能将被恢复。 – Prradep