[英]changing colors of box in boxplot in r using finalfit / hr_plot
[英]Plot the boxplot by using check box in shiny app
我想使用復選框輸入來顯示不同的島嶼級別(在為 x 軸選擇的分類變量內),帶有單獨的箱線圖和不同的 colors 帶有圖例。 但是,如果未選中此復選框,我只想顯示不帶 fill=legend 的箱線圖,即:
ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis)) +
geom_boxplot()
這個 R 代碼是我嘗試使用的代碼,但沒有用。 你能幫我解決或告訴我我的 R 代碼有什么錯誤嗎? 先感謝您
library(shiny)
library(palmerpenguins)
library(ggplot2)
library(dplyr)
penguin <- penguins
penguin$year <- as.factor(penguin$year)
ui <- fluidPage(
titlePanel("Data Visualisation of Penguins Data"),
sidebarPanel(
selectInput("yaxis",
label = "Choose a y-axis variable to display",
choices = list("bill_length_mm",
"bill_depth_mm",
"flipper_length_mm",
"body_mass_g"),
selected = "bill_length_mm"),
selectInput("xaxis",
label = "Choose a x-axis variable to display",
choices = c("species",
"sex",
"year"),
selected = "sex"),
checkboxGroupInput("islandlevels",
label = "Check to display different island levels",
choices = c("island"),
selected = NULL),
br(), br(),
selectInput("species",
label = "Choose species to view separate plot",
choices = list("Adelie",
"Chinstrap",
"Gentoo"),
selected = NULL)),
mainPanel(
plotOutput("plot1"),
br(), br(),
plotOutput("plot2")
)
)
server <- function(input, output){
dat <- reactive({
if(input$xaxis == "sex") penguin[!is.na(penguin$sex),] else penguin
})
output$plot1 <- renderPlot({
if(input$islandlevels == "island") {
req(penguin, input$xaxis, input$yaxis)
ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis, fill=island)) +
geom_boxplot()
}
if(input$islandlevels = NULL) {
req(penguin, input$xaxis, input$yaxis)
ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis)) +
geom_boxplot()}
})
}
shinyApp(ui = ui, server = server)
只要您不想要任何其他復選框輸入,您就可以使用checkboxInput
而不是checkboxGroupInput
,這使得檢查更容易一些。
服務器中的一個問題是您使用了island
而不是"island"
。 此外,您可以通過使用fill <- if (input$islandlevels) "island"
來稍微簡化您的代碼,這將返回NULL
是未選中復選框,否則返回"island"
。 這樣你就可以只用一個ggplot
語句處理這兩種情況。
完整的可重現代碼:
library(shiny)
library(palmerpenguins)
library(ggplot2)
library(dplyr)
penguin <- penguins
penguin$year <- as.factor(penguin$year)
ui <- fluidPage(
titlePanel("Data Visualisation of Penguins Data"),
sidebarPanel(
selectInput("yaxis",
label = "Choose a y-axis variable to display",
choices = list("bill_length_mm",
"bill_depth_mm",
"flipper_length_mm",
"body_mass_g"),
selected = "bill_length_mm"),
selectInput("xaxis",
label = "Choose a x-axis variable to display",
choices = c("species",
"sex",
"year"),
selected = "sex"),
checkboxInput("islandlevels",
label = "Check to display different island levels",
value = FALSE),
br(), br(),
selectInput("species",
label = "Choose species to view separate plot",
choices = list("Adelie",
"Chinstrap",
"Gentoo"),
selected = NULL)),
mainPanel(
plotOutput("plot1"),
br(), br(),
plotOutput("plot2")
)
)
server <- function(input, output){
dat <- reactive({
if(input$xaxis == "sex") penguin[!is.na(penguin$sex),] else penguin
})
output$plot1 <- renderPlot({
req(penguin, input$xaxis, input$yaxis)
fill <- if (input$islandlevels) "island"
ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis, fill = fill)) +
geom_boxplot()
})
}
shinyApp(ui = ui, server = server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.