【问题标题】:ggplot2 and Shiny conditional aesthetics based on selectInput?基于 selectInput 的 ggplot2 和 Shiny 条件美学?
【发布时间】:2021-01-13 12:57:09
【问题描述】:

我已经构建了一个闪亮的仪表板,其中一部分将允许人们生成与其地理区域相关的图表。区域将作为 selectInput 列在侧栏中,并在图表上绘制区域,但是,如果他们选择英格兰,我希望它具有不同的美感,尤其是更粗的线,以便突出。所有其他区域的厚度应相同,但颜色仍应不同,以便区分。我觉得我错过了一些非常明显的东西!

  dashboardHeader(title = "Dashboard"
  ),
  dashboardSidebar(
    sidebarMenu(id = "sidebarid", 
                               menuItem("Graphs", tabName = "graphs", icon = icon("chart-line")),
                               conditionalPanel(
                                 'input.sidebarid == "graphs"',
                                 selectInput("area", "Area",
                                             c("England", "Bolton","Bury","Manchester","Oldham", "Rochdale", "Salford","Stockport","Tameside","Trafford","Wigan"),
                                             multiple = T, selected = "Manchester")
                               )
                   )
  ),
  dashboardBody(
    ## Maps Tab, core dashboard
      tabItem(
        tabName = "graphs",
        fluidRow(
          width = "100%",
          plotOutput("chart"),
        )
      )
    )
  )

server <- 
  
  function(input,output, session){
    
    area <- reactive({
      NCMPRec_time %>% filter(AreaName %in% input$area)
    })
    
    output$chart <- renderPlot({
      ggplot(area(), aes(x=Timeperiod, y=Value, group = AreaName)) +
        geom_line(aes(color = AreaName)) +
        geom_point(aes(color = AreaName)) +
        labs(x = "Time", y = "Percent", col="Area") +
        theme_classic() +
        scale_colour_branded()
    })
    

  }

shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    您可以使行大小取决于input$area

    output$chart <- renderPlot({
          if (input$area == "England") {
            line_size <- 1
          } else {
            line_size <- 0.5
          }
          
          ggplot(area(), aes(x=Timeperiod, y=Value, group = AreaName)) +
            geom_line(aes(color = AreaName), size = line_size) +
            geom_point(aes(color = AreaName)) +
            labs(x = "Time", y = "Percent", col="Area") +
            theme_classic() +
            scale_colour_branded()
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      相关资源
      最近更新 更多