聚合数据框的ggplot是缺失值

时间:2017-08-02 13:15:25

标签: r dataframe ggplot2 shiny aggregate

我试图抓住一些合并数据,但遗憾的是我有一些损失...

df <-   Project    Subproject       Value      Date  
                 A           1              47      2017-08-04
                 A           2              22      2017-08-04
                 B           1              1       2017-08-04
                 A           1              40      2017-08-07
                 A           2              29      2017-08-07
                 B           1              1       2017-08-07

new_df <- df %>%
          group_by(Project, Date)%>%
          summarise(Value = sum(Value))


ui <- dashboardPage(
      dashboardSidebar(
        sidebarMenu(
        menuItem('Dashboard', tabName = 'dashboard', icon = icon('dashboard')),

  menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200)
  ),


  menuItem(
    checkboxGroupInput('project_name', label = "Project", choices = c(unique(new_df$Project)), selected = c(unique(new_df$Project)))

  ),

  menuItem(
    submitButton(text = "Apply Changes", icon = NULL, width = NULL)
  )
)
),

dashboardBody(

fluidRow(
  box(plotOutput("plot1", width = 1000, height = 500))
)
)

server <- function(input, output) {


output$plot1 <- renderPlot({


date_data <- reactive({
  subset(new_df, Date >= input$date_range[1] & Date <= input$date_range[2])
})


project_data <- reactive({
  subset(date_data(), Project %in% input$project_name)
})




ggplot(data = project_data(), aes(x = Date, y = value, fill = Project)) +
  geom_bar(stat = 'identity') +
  geom_hline(yintercept = 49, linetype = 'dotted', color = 'red', size = 0.8) + 
  geom_hline(yintercept = 70, linetype = 'dashed', color = 'purple', size = 0.8) +
  geom_text(data = project_data(), aes(label = value, fill = Project), size=4, position = position_stack(vjust = 0.5)) + 

 })
}
shinyApp(ui, server) 

从照片中可以看出,第一张照片是我的geom_bar,没有汇总/汇总数据,第二张是汇总/汇总。项目B在第二个项目中根本没有出现,尽管据称仍然在数据框中......并且在第一个图表中没有总结,我的geom_text仍然按子项目显示非聚合数字。

graphs

1 个答案:

答案 0 :(得分:0)

想出来......原来我的一些值实际上是NA而不是0,这搞乱了列表的聚合方式。用这条线固定:

df$Value = ifelse(is.na(df$Value), 0, df$Value)