Home > other >  plot by group in shiny
plot by group in shiny

Time:06-16

I am trying to make a plot in Shiny and I would like to be able to plot all the data set as well as group data sets based on by. How to get around this? Many thanks in advance. enter image description here

library(shiny)
library(ggplot2)
library(dplyr)


ui <- fluidPage(

    # Application title
    titlePanel("Plot By by"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        
      sidebarPanel( width = 2, selectInput(inputId = "by", 
                                           label = "dataset by",
                                           choices = c("by.1", "by.2"), 
                                           selected = "by.1")
                    ),

        
        
        
       
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
      
      
      df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
                       len = c(4.2, 10, 29.5, 5, 7, 15), 
                       by = c("by.1", "by.1","by.1","by.2","by.2","by.2"))

      
      mycols <- c("#92d050", "#57d3ff", "#ffc000")
      
      df <- df %>%
        arrange(desc(len)) %>%
        mutate(fills = ifelse(row_number() <= length(mycols), mycols, "grey50"))
      
      ggplot(data = df, aes(x = len, y = reorder(dose, len)))  
        geom_col(aes(fill = I(fills))) 
        geom_text(aes(x = len/2, label = glue::glue("{dose} ({len}%)")))  
        theme_minimal()  
        ## remove expansion and x title
        scale_x_continuous(NULL, expand = c(0,0))  
        # remove the y bits
        theme(axis.ticks.y = element_blank(), 
              axis.title.y = element_blank(), 
              axis.text.y = element_blank())
    })
    
    
}

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

CodePudding user response:

I'd suggest using multiple = TRUE for your selectInput - also note the filter() call:

library(shiny)
library(ggplot2)
library(dplyr)
library(shinyWidgets)

DF <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
                 len = c(4.2, 10, 29.5, 5, 7, 15), 
                 by = c("by.1", "by.1","by.1","by.2","by.2","by.2"))

ui <- fluidPage(
  titlePanel("Plot By by"),
  sidebarLayout(
    sidebarPanel( width = 2, selectizeInput(inputId = "by", 
                                         label = "dataset by",
                                         choices = c("by.1", "by.2"), 
                                         selected = "by.1",
                                         multiple = TRUE,
                                         options = list('plugins' = list('remove_button')))
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({

    mycols <- c("#92d050", "#57d3ff", "#ffc000")
    
    DF <- DF %>%
      arrange(desc(len)) %>%
      mutate(fills = ifelse(row_number() <= length(mycols), mycols, "grey50")) %>% filter(by %in% input$by)
    
    ggplot(data = DF, aes(x = len, y = reorder(dose, len)))  
      geom_col(aes(fill = I(fills))) 
      geom_text(aes(x = len/2, label = glue::glue("{dose} ({len}%)")))  
      theme_minimal()  
      ## remove expansion and x title
      scale_x_continuous(NULL, expand = c(0,0))  
      # remove the y bits
      theme(axis.ticks.y = element_blank(), 
            axis.title.y = element_blank(), 
            axis.text.y = element_blank())
  })
}

shinyApp(ui = ui, server = server)

result

PS: as an alternative to the selectizeInput please check shinyWidgets::pickerInput with its select/deselect all option.

  • Related