[英]R Shiny: Displaying Boxplot (using ggplot) shows no correct Boxplot
我正在嘗試使用 ggplot 和 geom_boxplot 來顯示和分組箱線圖。 該代碼沒有顯示錯誤並顯示圖表,但顯示正確的箱線圖時出現問題。 如您所見,沒有箱線圖,但對於所選變量的每個類別都有一條線。 我想要的是這樣的 output:
使用的變量保存為 data.frames。 你知道如何修復箱線圖 output 嗎?
我的代碼:
library(shiny)
library(shinydashboard)
library(dplyr)
library(DT)
library(ggplot2)
library(likert)
levels.netusoft <- c("Sehr wenig", "Etwas", "Stark", "Sehr stark", "Verweigert",
"Weiß nicht", "Keine Antwort")
levels.ppltrst <- c("1", "2", "3", "4", "5", "6", "Verweigert", "Weiß nicht",
"Keine Antwort")
levels.polintr <- c("Überhaupt nicht", "Sehr wenig", "Etwas", "Stark", "Sehr stark",
"Verweigert", "Weiß nicht", "Keine Antwort")
levels.psppsgva <- c("Überhaupt nicht fähig", "Wenig fähig", "Ziemlich fähig",
"Sehr fähig", "Vollkommen fähig", "Verweigert", "Weiß nicht",
"Keine Antwort")
levels.actrolga <- c("Wenig fähig", "Ziemlich fähig", "Sehr fähig", "Vollkommen fähig",
"Verweigert", "Weiß nicht", "Keine Antwort")
levels.gndr <- c("männlich", "weiblich")
dataset <- data.frame("netusoft" = factor(sample(levels.netusoft, 100,
replace = TRUE),
levels.netusoft),
"ppltrst" = factor(sample(levels.ppltrst, 100,
replace = TRUE),
levels.ppltrst),
"polintr" = factor(sample(levels.polintr, 100,
replace = TRUE),
levels.polintr),
"psppsgva" = factor(sample(levels.psppsgva, 100,
replace = TRUE),
levels.psppsgva),
"actrolga" = factor(sample(levels.actrolga, 100,
replace = TRUE),
levels.actrolga),
"gndr" = factor(sample(levels.gndr, 100,
replace = TRUE),
levels.gndr),
check.names = FALSE)
# ----- UI
ui <- fluidPage(
dashboardPage(
dashboardHeader(title = "Test Shiny Dashboard",
titleWidth = 300),
dashboardSidebar(width = 300,
selectInput(inputId = "round",
label = "Wählen Sie eine Runde aus",
c("Runde 9" = "9"),
selected = "9", selectize = FALSE),
#end selectinput
conditionalPanel(
condition = "input.round == '9'",
selectInput(inputId = "battery",
label = "Wählen Sie Themenfeld aus",
c("A: Medien-, Internetnutzung, Soziales Vertrauen" = "A",
"B: Politische Variablen, Immigration" = "B"),
selectize = FALSE), #end selectinput
uiOutput("question_placeholder")
),
checkboxInput(
inputId = "group",
label = "Daten gruppieren",
value = FALSE), #end checkbox
conditionalPanel(
condition = "input.group == true",
selectInput(
inputId = "UV",
label = "Daten gruppieren nach:",
c("Geschlecht" = "gndr")
) # end conditionalPanel
)
), # end dashboardSidebar
dashboardBody(
fluidRow(
box(width = 8, status = "info", solidHeader = TRUE,
title = "Graph:",
plotOutput("plot", width = "auto", height = 500)
)
), # end fluidRow
) #end dashboardBody
)
)
server <- function(input, output, session) {
get_data <- reactive({
req(input$question)
if (input$group) {
dataset %>%
select(Antwortkategorie = input$question, req(input$UV)) %>%
group_by(grp = !!as.symbol(input$UV), Antwortkategorie)
} else {
dataset %>%
select(Antwortkategorie = input$question) %>%
group_by(Antwortkategorie)
}
})
output$question_placeholder <- renderUI({
if (input$battery == "A") {
choices <- c("A2|Häufigkeit Internetnutzung" = "netusoft",
"A4|Vertrauen in Mitmenschen" = "ppltrst")
} else if (input$battery == "B") {
choices <- c("B1|Interesse an Politik" = "polintr",
"B2|Politische Mitsprachemöglichkeit" = "psppsgva",
"B3|Fähigkeit politischen Engagements " = "actrolga")
}
selectInput(inputId = "question",
label = "Wählen Sie eine Frage aus",
choices,
selectize = FALSE)
})
theplot <- reactive({
p <- get_data() %>%
ggplot(mapping = aes(y = (Antwortkategorie))) +
geom_boxplot()
if(input$group==TRUE) {
p <- get_data() %>%
ggplot(mapping = aes(x=grp,
y=Antwortkategorie)) +
geom_boxplot()
}
plot(p)
})
output$plot <- renderPlot({
theplot()
})
}
shinyApp(ui, server)
更新
您的數據需要重新編碼為數字。 如果您使用此示例數據框,您的代碼可以工作,並且您可以顯示每個變量的箱線圖。
dataset <- data.frame("netusoft" = sample(c(1:4), 100,
replace = TRUE),
"ppltrst" = sample(c(1:6), 100,
replace = TRUE),
"polintr" = sample(c(1:5), 100,
replace = TRUE),
"psppsgva" = sample(c(1:5), 100,
replace = TRUE),
"actrolga" = sample(c(1:4), 100,
replace = TRUE),
"gndr" = factor(sample(c("männlich", "weiblich"), 100,
replace = TRUE)),
check.names = FALSE)
初步答案
您忘記為geom_boxplot()
計算框所需的 x 軸定義美學。
theplot <- reactive({
p <- get_data() %>%
ggplot(mapping = aes(x = "your_data_here", = (Antwortkategorie))) +
geom_boxplot()
此外,您錯誤地定義了分組變量。 在你的 if 語句中。 如果要按組繪制箱線圖,則必須在aes()
中使用group =
參數。
if(input$group==TRUE) {
p <- get_data() %>%
ggplot(mapping = aes(x="your_data_here",
y=Antwortkategorie,
group=grp)) +
geom_boxplot()
}
plot(p)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.