聚合数据帧的ggplot缺失值 [英] ggplot of aggregated data frame is missing values

查看:166
本文介绍了聚合数据帧的ggplot缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  df< p>< p> - 项目子项目日期
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(项目,日期)%>%
汇总(Value = sum(Value))


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

menuItem(
dateRangeInput('date_range',label =日期范围,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))



服务器< - 函数(输入,输出){


输出$ plot1< - renderPlot({


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


project_data < - 反应性({
子集(date_data(),项目%输入$ project_name)
})




ggplot(data = project_data(),aes(x = Date,y = value,fill =项目))+
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仍然显示了子项目中未汇总的数字。



我的一些价值实际上是NA而不是0,这正在搞乱名单如何聚合。用这一行修正:

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


I'm trying to ggplot some consolidated data but unfortunately I'm having some loss...

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) 

As you can see from the photos, the first picture is my geom_bar without aggregating/summarising data, and the second is with aggregating/summarising. Project B is not showing up at all in the second one, despite supposedly still being in the dataframe... and without summarising in the first graph, my geom_text is still displaying non-aggregated numbers by subproject.

解决方案

Figured it out... Turns out that some of my values were actually NA instead of 0, which was messing up how the list was aggregating. fixed with this line:

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

这篇关于聚合数据帧的ggplot缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆