【问题标题】:Stack Bar Plot For R Shiny DashboardR Shiny仪表板的堆栈条图
【发布时间】:2018-09-19 01:09:34
【问题描述】:

如何为我的 Shiny Dashboard 制作堆叠条形图? 例如,我想要一个 2016 年的条形图,其中包含申请数量、接受的申请数量和注册人数,但所有这些值都在一列中 - 堆叠。

this is my dataset

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您可以使用ggplot2 包创建带有geom_bar(stat = "identity") 的堆叠条形图。但是要将wide data.frame 格式转换为narrow 所需的ggplot2 格式,则需要使用reshape2 包的melt 函数。

    Shiny Dashboard 环境中的条形图请参见下面的代码:

    # load the required packages
    library(shiny)
    require(shinydashboard)
    library(ggplot2)
    library(dplyr)
    
    df <- read.table(text = "
    Enrolment Applications Accepted Students Enrolled 
                     3 2017 30 25 5 20 
                     2 2016 24 21 3 20 
                     1 2015 22 20 2 17") 
    
    
    #Dashboard header carrying the title of the dashboard
    header <- dashboardHeader(title = "Basic Dashboard")  
    
    #Sidebar content of the dashboard
    sidebar <- dashboardSidebar(
      sidebarMenu(
        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
        )
    )
    
    
    frow2 <- fluidRow(
    
      box(
        title = "Enrollement"
        ,status = "primary"
        ,solidHeader = TRUE 
        ,collapsible = TRUE 
        ,plotOutput("enrollement", height = "300px")
      )
    
    )
    
    
    
    # combine the two fluid rows to make the body
    body <- dashboardBody(frow2)
    
    #completing the ui part with dashboardPage
    ui <- dashboardPage(title = 'This is my Page title', header, sidebar, body, skin='red')
    
    # create the server functions for the dashboard  
    server <- function(input, output) { 
      #creating the plotOutput content
    
      output$enrollement <- renderPlot({
        df$Enrolment <- factor(df$Enrolment)
        df$Not_Accepted <- df$Applications - df$Accepted
        df$Not_Enrolled <- df$Accepted - df$Enrolled
        df2 <- melt(df[, c("Enrolment", "Enrolled", "Not_Enrolled", "Not_Accepted")])
    
        ggplot(df2, aes(Enrolment, y = value, fill = variable)) +
          geom_bar(stat = "identity")
      })
    }
    
    
    shinyApp(ui, server)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 2021-05-14
      • 1970-01-01
      • 2016-06-04
      • 2015-12-25
      • 2020-07-09
      • 2023-04-08
      • 2016-08-14
      • 2017-09-27
      相关资源
      最近更新 更多