簡體   English   中英

R Shiny 儀表板的堆棧條形圖

[英]Stack Bar Plot For R Shiny Dashboard

如何為 Shiny Dashboard 制作堆棧條形圖? 例如,我想要一個 2016 年的條形圖,其中包含申請數量、接受的申請數量和注冊數量,但所有這些值都放在一列中 - 堆疊。

這是我的數據集

您可以使用ggplot2包創建帶有geom_bar(stat = "identity")堆疊條形圖。 然而轉換data.frame格式轉換成由所需要的一個ggplot2這里有必要使用melt的功能reshape2包。

請參閱下面的代碼以獲取 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)

輸出:

閃亮的儀表板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM