首页 文章

使用闪亮创建ggplot条形图时丢失的条形图

提问于
浏览
0

我使用了闪亮并创建了一个 app.R 文件,希望用 ggplot 构建一个条形图 . 我还使用 checkboxGroupInput 创建了2个复选框来控制条件 . 虽然在检查所有方框后,条的总数应为28,但由于某种原因,最大值似乎只允许17条 . 因此缺少一些条形(数据行) . 丢失的条似乎没有模式 . 有人可以帮忙吗?

数据集:https://drive.google.com/open?id=1fUQk_vMJWPwWnIMbXvyd5ro_HBk-DBfc

我的代码:

midterm <- read.csv('midterm-results.csv')

library(dplyr)
library(tidyr)

# get column number for response time
k <- c(33:88)
v <- c()

for (i in k){
        if (i%%2 == 1){
                v <- c(v,i)
        }
}


#average response time by question
time <- midterm[ , v]
new.col.name <- gsub('_.*', "", colnames(time))
colnames(time) <- new.col.name
avg.time <- data.frame(apply(time, 2, mean))
avg.time$question <- rownames(avg.time)
colnames(avg.time) <- c('response_time', 'question')
rownames(avg.time) <- NULL
avg.time$question <- factor(avg.time$question, 
                            levels = c('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8.9',
                                       'Q10','Q11','Q12.13','Q14','Q15','Q16','Q17',
                                       'Q18','Q19','Q20','Q21','Q22','Q23','Q24','Q25',
                                       'Q26','Q27','Q28','Q29','Q30'))

avg.time$question_type <-  c(1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0)
# I did this manually because the there when data was imported into the  midterm.csv,
# q8 & 9, q12 &13 were accidentally merged (28 v.s 30 question)

avg.time$question_type <- ifelse(avg.time$question_type == 1,
                                 'googleable', 'not googleable')
avg.time$question_type <- factor(avg.time$question_type,
                                 levels = c('googleable', 'not googleable'))


library(shiny)
library(ggplot2)


ui <- fluidPage(
    checkboxGroupInput(inputId = "type",
                       label = "select question type", 
                       choices = levels(avg.time$question_type),
                       selected = TRUE), 
    plotOutput('bar')
    )

server <- function(input, output) {
    output$bar <- renderPlot({
         ggplot(avg.time[avg.time$question_type==input$type, ], 
                aes(x=question, response_time)) +
             geom_bar(aes(fill = question_type), stat='identity', width = 0.5)
    }, height =500, width = 1000)
}

shinyApp(ui = ui, server = server)

1 回答

  • 1
    library(shiny)
    library(ggplot2)
    
    
    ui <- fluidPage(
         checkboxGroupInput(inputId = "type", label = "select question type", 
                     choices = levels(avg.time$question_type), selected = TRUE), 
         plotOutput('bar')
    )
    
    server <- function(input, output) {
    
      data <- reactive(avg.time[avg.time$question_type %in% input$type, ])
      output$bar <- renderPlot({
      ggplot(data(), 
           aes(x=question, response_time)) + geom_bar(stat='identity', width = 0.5, 
                                                      aes(fill = question_type))
    
     }, height =500, width = 1000)
    }
    
    shinyApp(ui = ui, server = server)
    

    当然你可以在ggplot2中使用avg.time [avg.time $ question_type%in%input $ type,]但反应性更好 .

相关问题