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. 
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)
