如何移除R Shiny中Plotly图表的ePCM图例?
移除Plotly散点图中ePCM图例的解决方案
我制作了一张与奥地利电动汽车市场相关的图表,已通过Shiny部署为Web应用(链接:https://gibraltar.shinyapps.io/esim-v2/),绘图使用Plotly工具。当前遇到的问题是无法移除右侧的ePCM图例。
以下是我的服务器端代码:
# Define the server logic server <- function(input, output) { # Filter data based on selected car model filteredData <- reactive({ if (input$carModel == "Alle") { dataSelection } else { dataSelection[dataSelection$CompleteModelName == input$carModel, ] } }) # Render the plotly plot output$plot <- renderPlotly({ evPlot <- plot_ly( data = filteredData(), x = ~Reichweitenkosten, y = ~RLL, text = ~CompleteModelName, color = ~ePCM, size = ~ePCM, type = "scatter", mode = "markers" ) # Update x-axis and y-axis range to keep them fixed evPlot <- evPlot %>% layout( xaxis = list(range = c(min(dataSelection$Reichweitenkosten), max(dataSelection$Reichweitenkosten)*1.1)), yaxis = list(range = c(min(dataSelection$RLL), max(dataSelection$RLL)*1.1)) ) evPlot }) }
我尝试过多个相关解决方案,但仍无法移除图例,恳请协助解决。
解决方法
你可以通过以下两种方式移除ePCM图例:
方式1:在plot_ly函数中直接设置隐藏图例
在初始化散点图时添加showlegend = FALSE参数,直接关闭所有图例:
evPlot <- plot_ly( data = filteredData(), x = ~Reichweitenkosten, y = ~RLL, text = ~CompleteModelName, color = ~ePCM, size = ~ePCM, type = "scatter", mode = "markers", showlegend = FALSE # 添加此行隐藏图例 )
方式2:在layout中全局关闭图例
在布局配置里加入showlegend = FALSE,同样可以隐藏所有图例:
evPlot <- evPlot %>% layout( xaxis = list(range = c(min(dataSelection$Reichweitenkosten), max(dataSelection$Reichweitenkosten)*1.1)), yaxis = list(range = c(min(dataSelection$RLL), max(dataSelection$RLL)*1.1)), showlegend = FALSE # 添加此行隐藏图例 )
由于你同时用color和size映射了ePCM字段,上述两种方式都能全局移除右侧的ePCM图例,任选其一即可生效。
内容的提问来源于stack exchange,提问作者Gibraltar
相关产品推荐
相关产品推荐

