Home > OS >  R Shiny: Displaying Boxplot (using ggplot) shows no correct Boxplot
R Shiny: Displaying Boxplot (using ggplot) shows no correct Boxplot

Time:08-17

I´m trying to display and group a boxplot by using ggplot and 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. enter image description here

The used variables are saved as data.frames. Do you know how to fix the boxplot 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)

CodePudding user response:

What you're seeing is not lines, but boxplots in the vertical direction, when you appear to want them in the horizontal direction.

The geom_boxplot function has an argument "outlier" indicate whether x or y represent separate boxes. Adding orientation = "y" to your geom_boxplot() call should get the plot that you want.

CodePudding user response:

You forgot to define the aesthetic for your x-axis that geom_boxplot() needs to calculate the box.

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 you want boxplots by group you have to use the group = parameter within aes().

if(input$group==TRUE) {
  p <- get_data() %>%
    ggplot(mapping = aes(x="your_data_here", 
                         y=Antwortkategorie,
                         group=grp))   
    geom_boxplot()
  
  
}

plot(p)
})
  • Related