反应性分组条形图取决于选择输入选择

时间:2018-06-15 15:40:40

标签: r ggplot2 shiny bar-chart reactive

我想制作一个以月为填充的配对条形图。然而,月份是一个selectinput选项,我不确定如何将其合并到ggplot命令中。使用fill = input $ Month或fill =“Reconciled.Month”将图形留空。该代码仅工作了一个月,但我希望能够比较一段时间内的性能变化,看看因果行为是否有任何变化,如果是,那么它们是好还是坏。 数据样本:

Provider    Reconciled Month    Per Visit Bill Rate- w/Mal  Per Visit Bill Rate Location
Andrews 1   27.68   30.57   Illinois
Davids  2   25.08   29.08   New York
Stein   3   31.39   31.39   New York
Goodman 2   26.00   30.00   Wyoming
Roberts 3   24.34   25.74   Utah
Buckley 1   29.54   33.54   Ohio
Castro  3   0.00    0.00    Ohio

代码:

  library(shiny)
    library(shinydashboard)
    library(ggplot2)
    library(rsconnect)
    library(lubridate)
    library(scales)
    library(plyr)
    library(dplyr)
    library(RColorBrewer)

AdjustedData<- read.csv("Adjusted Data.csv")
Target <- data.frame(yintercept= 40.00, Target= factor(40.00))
AdjustedData$Percentage <- round(AdjustedData$Total.Procedure/AdjustedData$Reconciled.Visits*100, 2)
AdjustedData$Percentage[is.na(AdjustedData$Percentage)] <- 0
opt <- read.csv("optimization.csv")
opt$OptPer <- opt$Optimization*100
opt$Location <- as.character(opt$Location)
opt$Location <- ifelse(opt$Location == "New Jersey" | opt$Location == "Indiana" | opt$Location == "Pennsylvania", "New Jersey/Indiana/Pennsylvania", opt$Location)
opt$Location <- as.factor(opt$Location)
mu <- mean(opt$OptPer) 
res <- as.vector(opt$OptPer-mu)
opt <- cbind(opt, res)


ui <- dashboardPage(
  dashboardHeader(title = "Performance Metrics"), 
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Bill Rate information", tabName = "Bill Rate Information", icon = icon("dollar-sign")),
      menuItem("Optimization Rates", tabName = "Optimization Rates", icon = icon("whmcs"))
    )
  ), 
  dashboardBody(
    tabItem( tabName = "dashboard"),
    tabItem( tabName = "Bill Rate Information",
    fluidRow(
            box(title = "Options",
            selectInput(inputId = ".Month",
                            label = "Month:", 
                            choices = c("January" = "1", "February" = "2", "March" = "3"), multiple = TRUE),
            selectInput(inputId = "Location",
                        label = "Location:", 
                        choices = c("Illinois", "Wound MD", "Ohio", "New York")), width = 1000, height = 200), multiple = TRUE,
      box(plotOutput("barplot", height = 500, width= 800)),
      box(plotOutput("barplot2", height= 500, width = 800))),
      box(plotOutput("barplot3", height = 500, width = 800))
    )))

server <- function(input, output) {

data <- reactive({
  dataset <- subset(AdjustedData, Location == input$Location & Reconciled.Month == input$Month)
})
data2 <- reactive({
  dataset2 <- subset(opt, Location == input$Location & Reconciled.Month == input$Month)
})

 output$barplot <-renderPlot({
    ggplot(data= data(), aes_string(x="Provider",y= "Per.Visit.Bill.Rate")) +
   geom_bar(stat="identity", position = "dodge")+
    labs(caption = "Data sourced from Provider Compensation Analysis and Monthly Summary \nof Billed Physician Encounters Reports. Rates have been adjusted to exclude \nthe deduction per visit for malpractice insurance where applicable.")+
   theme_light()+
   theme(plot.caption = element_text(size= 8, hjust = .5))+
    theme(axis.text.x=element_text(angle= 45, vjust=.5)) +
    ggtitle("Per Visit Bill Rate", input$Location)+
   theme(plot.title = element_text(size = 22, hjust = .5, family = "serif"))+
    theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif"))+
   geom_text(aes(label=sprintf("$%.2f",Per.Visit.Bill.Rate)), position=position_dodge(width=0.9),hjust= .5, vjust=0,angle= 0) +
   scale_y_continuous(name = "Per Visit Bill Rate ($)", limits = c(0,100),breaks =seq(0,100,10), labels = dollar)+
   scale_color_continuous()+
   geom_hline(aes(yintercept = yintercept , linetype = Target), data = Target, color = "red", size = 1.0)+
   guides(fill=guide_legend(title = "Per Visit Bill Rate ($)", reverse = TRUE))+
      theme(plot.title = element_text(hjust = 0.5))
  })
 output$barplot2 <-renderPlot({
    ggplot(data= data(), aes_string(x="Provider",y= "Percentage", fill= "Reconciled.Month")) + 
     geom_bar(stat = "identity")+
     geom_text(aes(x=Provider, y= Percentage, label=sprintf("%.1f%%", Percentage)), position=position_dodge(width=0.9),hjust= .5, vjust= 0 ,angle= 0)+
     labs(caption = "Data sourced from Provider Compensation Analysis and \nMonthly Summary of Billed Physician Encounters Reports, March 2018 \nRates have been calculated using the sum of all procedure codes.")+
   scale_y_continuous(name = "Percentage (%)", limits = c(0,100),breaks =seq(0,100,10))+
   scale_color_continuous()+ 
     theme(plot.caption = element_text(size= 8, hjust = .5))+
     theme(axis.text.x=element_text(angle= 0, vjust=.5))+ 
     ggtitle("Procedure Percentages", input$Location)+
     theme(plot.title = element_text(size = 22, hjust = .5, family = "serif"))+
     theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif"))+
     guides(fill=guide_legend(title = "Month", reverse =FALSE))
       })
  output$barplot3 <- renderPlot({
    ggplot(data= data2(), aes_string(x = "Provider", y= "OptPer", fill= "OptPer"))+
      geom_bar(stat = "identity") + 
      geom_text(aes(x= Provider, y= OptPer, label=sprintf("%.1f%%", OptPer)), position = position_dodge(width=.6), hjust= .5, vjust=0)+
      theme_light()+
      theme(axis.text.x=element_text(angle= 45, vjust=.5))+
      scale_color_continuous()+
      ggtitle("Optimization Percentages", input$Location)+ 
      labs(caption = "Data sourced from Provider Compensation Analysis Reports, March 2018")+ 
      theme(plot.caption = element_text(size= 8, hjust = .5))+ 
      ylab("Optimization Percentage")+
      theme(plot.title = element_text(size = 22, hjust = .5, family = "serif"))+
      theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif"))+ 
      scale_y_continuous(limits= c(0,100), labels = function(x) paste0(x, "%")) + 
      guides(fill=guide_legend(title = "Optimization Percentage (%)", reverse = TRUE))
  })
}
shinyApp(ui = ui, server = server)

0 个答案:

没有答案
相关问题