简体   繁体   English

从图例中删除美学 ggplotly R Shiny

[英]Remove an aesthetic from a legend ggplotly R Shiny

I have a shiny dashboard with a graph - certain bars on the graph are highlighted based on a corresponding picker input, as you can see in this gif .我有一个带有图表的 shiny 仪表板 - 图表上的某些条形图根据相应的选择器输入突出显示,如您在此 gif中所见。

I only want the legend to reflect the fill, not the colour.我只希望图例反映填充,而不是颜色。 I know how to do this in ggplot, but how can I accomplish this in a ggplotly object?我知道如何在 ggplot 中执行此操作,但是如何在ggplotly object 中完成此操作?

I've tried setting the guide to False in scale_color_manual, as well as setting guide(color = False) , but no luck.我尝试在 scale_color_manual 中将guide设置为False ,以及设置guide(color = False) ,但没有运气。

Also, of low priority, if anyone could give me an example of only having the outline around the whole of the stacked bar, not each individual segment, that would be appreciated.此外,低优先级,如果有人能给我一个例子,只在整个堆叠条周围有轮廓,而不是每个单独的段,那将不胜感激。

Reproducible example:可重现的例子:

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(plotly)

dat <- data.frame(
  location = rep(c("Loc1", "Loc2", "Loc3"), each = 3),
  category = rep(c("cat1", "cat2", "cat3"), 3),
  value = runif(9, 20, 50)
)


ui <- fluidPage(

  
    sidebarLayout(
        sidebarPanel(
            pickerInput(
              inputId = "selected",
              label = "Select a category:",
              choices = c("Loc1", "Loc2", "Loc3")
            )
        ),

 
        mainPanel(
           plotlyOutput("outputPlot")
        )
    )
)


server <- function(input, output) {
  output$outputPlot <- renderPlotly({
    dat_selected <- dat %>%
      # Mutate flag based on selected location
      mutate(selected = ifelse(location == input$selected, 1, 0)) %>%
      ggplot(
        aes(
          x = value,
          y = location,
          group = category,
          fill = category,
          color = as.factor(selected)
        )
      ) + geom_bar(stat = "identity") +
      scale_fill_manual(values = c("yellow", "white", "blue")) +
      scale_color_manual(values = c("white", "red"), guide = "none") +
      guides(color = F)
    
    ggplotly(dat_selected)
  })
    
}

# Run the application 
shinyApp(ui = ui, server = server)

Note that logical values to scale arguments in guides are deprecated.请注意,不推荐使用guides中缩放 arguments 的逻辑值。 In ggplot2 you would use guides(color = "none") instead.在 ggplot2 中,您将使用guides(color = "none")代替。

In ggplotly you may select traces shown in the legend using the traces argument in style() .在 ggplotly 中,您可以使用style()中的traces参数在图例中显示 select 轨迹。

Replace ggplotly(dat_selected) by替换ggplotly(dat_selected)

ggplotly(dat_selected) %>%
  style(showlegend = FALSE, traces = c(2, 4, 6))

to get要得到

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM