[英]R Shiny: Displaying Boxplot (using ggplot) shows no correct Boxplot
I´m trying to display and group a boxplot by using ggplot and geom_boxplot.我正在尝试使用 ggplot 和 geom_boxplot 来显示和分组箱线图。 The code shows no error and displays a graph but there is a problem displaying a correct boxplot.该代码没有显示错误并显示图表,但显示正确的箱线图时出现问题。 As you can see there is no boxplot but for every category of the selected variable a line.如您所见,没有箱线图,但对于所选变量的每个类别都有一条线。 What I want is an output like this:我想要的是这样的 output:
The used variables are saved as data.frames.使用的变量保存为 data.frames。 Do you know how to fix the boxplot output?你知道如何修复箱线图 output 吗?
My code:我的代码:
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)
Update更新
Your data needs to be recoded into numbers.您的数据需要重新编码为数字。 If you use this example data frame your code works and you can display boxplots for each variable.如果您使用此示例数据框,您的代码可以工作,并且您可以显示每个变量的箱线图。
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)
Initial Answer初步答案
You forgot to define the aesthetic for your x-axis that geom_boxplot()
needs to calculate the box.您忘记为geom_boxplot()
计算框所需的 x 轴定义美学。
theplot <- reactive({
p <- get_data() %>%
ggplot(mapping = aes(x = "your_data_here", = (Antwortkategorie))) +
geom_boxplot()
Additionally, you defined your grouping variable wrong.此外,您错误地定义了分组变量。 in your if-statement.在你的 if 语句中。 If you want boxplots by group you have to use the group =
parameter within aes()
.如果要按组绘制箱线图,则必须在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.