我正在嘗試在 Shiny 中繪制一個圖,我希望能夠繪制所有資料集以及基于by. 如何解決這個問題?提前謝謝了。
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)
uj5u.com熱心網友回復:
我建議multiple = TRUE您使用selectInput- 還要注意filter()電話:
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)

PS:作為selectizeInput請檢查shinyWidgets::pickerInput的選擇/取消選擇所有選項的替代方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493120.html
上一篇:Shinyapp中的多個地塊
下一篇:繪制GAM(繪圖輸出中的錯誤)
