Reputation: 2861
I have a plot similar to this and I would like to see each selected plot vertically, using facet_wrap
or par()
, I guess.
library(shiny)
library(ggplot2)
library(dplyr)
library(shinyWidgets)
DF <- data.frame(dose = c("D1", "D2", "D3", "D1", "D2", "D3", "D1", "D2", "D3"),
len = c(4.2, 10, 29.5, 5, 7, 15, 20, 50, 40),
by = c("by.1", "by.1","by.1","by.2","by.2","by.2", "by.3", "by.3", "by.3"))
ui <- fluidPage(
titlePanel("Plot By by"),
sidebarLayout(
sidebarPanel( width = 2, selectizeInput(inputId = "by",
label = "dataset by",
choices = c("by.1", "by.2", "by.3"),
selected = "by.1",
multiple = T,
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)
Expected Answer;
Plot 1
Plot 2
Plot 3
Upvotes: 0
Views: 42
Reputation: 79184
Just add this line to your code: facet_wrap(.~by, ncol=1) +
library(shiny)
library(ggplot2)
library(dplyr)
library(shinyWidgets)
DF <- data.frame(dose = c("D1", "D2", "D3", "D1", "D2", "D3", "D1", "D2", "D3"),
len = c(4.2, 10, 29.5, 5, 7, 15, 20, 50, 40),
by = c("by.1", "by.1","by.1","by.2","by.2","by.2", "by.3", "by.3", "by.3"))
ui <- fluidPage(
titlePanel("Plot By by"),
sidebarLayout(
sidebarPanel( width = 2, selectizeInput(inputId = "by",
label = "dataset by",
choices = c("by.1", "by.2", "by.3"),
selected = "by.1",
multiple = T,
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}%)"))) +
facet_wrap(.~by, ncol=1) +
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)
Upvotes: 2