2017-08-02 176 views
0

我想ggplot一些綜合數據,但不幸的是我有一些損失......ggplot彙總數據幀的丟失值

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

回答

0

想通了......原來,我的一些價值觀實際上是NA,而不是0,這是搞亂名單是如何聚集。用這條線固定:

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

啊,太棒了! :) – Aramis7d