闪亮的情节堆积条形图不显示酒吧

时间:2018-04-11 19:30:46

标签: r ggplot2 shiny dplyr plotly

尝试绘制堆积条形图。我得到了空白切面的瓷砖,但没有酒吧。我的所有其他图表都运行正常,包括其他堆积条形图。无法弄清楚我哪里出错了。

数据:

data <- structure(list(Amount_USD__c = c(11100, 336, 5940, 15654, 2424, 
2988, 2903.333, 336, 10935, 5148, 15750, 2390.4, 1788, 348, 228, 
336, 2390.4, 1872, 2000, 4875, 2000, 1497.6, 1428, 635.426269, 
7500, 1740, 0, 2700, 2000, 1068, 2202.192448, 37485, 0, 588, 
2400, 336, 1046.4, 24768, 0, 1132.8, 2390.4, 2390.4, 1718.4, 
336, 348, 2187, 3010.962241, 55000, 4296, 360, 1560.001, 1046.4, 
1998, 2187, 5388, 2148, 336, 2187, 6480, 1200, 2210, 21090, 2388, 
708, 1908, 588, 1188, 4356, 336, 5917.6, 336, 348, 0, 2764.8, 
4720.14, 2388, 500, 20124, 6629.9, 5247, 4118.4, 2388, 3900, 
900, 2880, 9561.6, 348, 348, 24000, 2388, 12500, 2511, 0, 2688, 
348, 336, 0, 3840, 348, 4800), StageName = c("2. Qualify", "1. Discovery", 
"1. Discovery", "5. Negotiate", "3. Analyze", "4. Evaluate", 
"1. Discovery", "1. Discovery", "2. Qualify", "1. Discovery", 
"1. Discovery", "3. Analyze", "3. Analyze", "1. Discovery", "1. Discovery", 
"1. Discovery", "1. Discovery", "4. Evaluate", "3. Analyze", 
"2. Qualify", "2. Qualify", "2. Qualify", "3. Analyze", "1. Discovery", 
"4. Evaluate", "1. Discovery", "1. Discovery", "3. Analyze", 
"2. Qualify", "1. Discovery", "1. Discovery", "1. Discovery", 
"1. Discovery", "1. Discovery", "2. Qualify", "1. Discovery", 
"4. Evaluate", "3. Analyze", "2. Qualify", "2. Qualify", "1. Discovery", 
"6. Sale Pending", "1. Discovery", "1. Discovery", "4. Evaluate", 
"1. Discovery", "1. Discovery", "2. Qualify", "2. Qualify", "1. Discovery", 
"1. Discovery", "2. Qualify", "4. Evaluate", "4. Evaluate", "4. Evaluate", 
"2. Qualify", "1. Discovery", "1. Discovery", "2. Qualify", "1. Discovery", 
"4. Evaluate", "1. Discovery", "3. Analyze", "2. Qualify", "2. Qualify", 
"1. Discovery", "1. Discovery", "3. Analyze", "1. Discovery", 
"1. Discovery", "1. Discovery", "1. Discovery", "1. Discovery", 
"3. Analyze", "2. Qualify", "1. Discovery", "1. Discovery", "3. Analyze", 
"2. Qualify", "3. Analyze", "2. Qualify", "1. Discovery", "4. Evaluate", 
"3. Analyze", "1. Discovery", "3. Analyze", "1. Discovery", "1. Discovery", 
"1. Discovery", "1. Discovery", "1. Discovery", "5. Negotiate", 
"2. Qualify", "1. Discovery", "1. Discovery", "2. Qualify", "1. Discovery", 
"1. Discovery", "1. Discovery", "3. Analyze"), CloseDate = structure(c(17711, 
17651, 17711, 17651, 17650, 17712, 17641, 17651, 17774, 17657, 
17774, 17682, 17651, 17634, 17634, 17651, 17709, 17682, 17651, 
17669, 17682, 17711, 17704, 17682, 17648, 17626, 17651, 17640, 
17712, 17657, 17651, 17794, 17651, 17634, 17682, 17647, 17651, 
17651, 17672, 17648, 17651, 17620, 17651, 17641, 17712, 17656, 
17651, 17705, 17739, 17641, 17651, 17651, 17651, 17651, 17655, 
17643, 17634, 17662, 17654, 17640, 17660, 17710, 17648, 17682, 
17651, 17634, 17634, 17682, 17647, 17712, 17712, 17634, 17651, 
17682, 17712, 17635, 17682, 17651, 17712, 17641, 17667, 17743, 
17662, 17648, 17643, 17711, 17681, 17634, 17711, 17651, 17712, 
17651, 17651, 17634, 17651, 17634, 17804, 17651, 17647, 17651
), class = "Date"), Rep = c("Rep 70", "Rep 4", "Rep 49", "Rep 29", 
"Rep 60", "Rep 23", "Rep 69", "Rep 49", "Rep 64", "Rep 16", "Rep 54", 
"Rep 6", "Rep 14", "Rep 45", "Rep 28", "Rep 21", "Rep 46", "Rep 56", 
"Rep 40", "Rep 60", "Rep 62", "Rep 68", "Rep 13", "Rep 37", "Rep 40", 
"Rep 30", "Rep 54", "Rep 56", "Rep 56", "Rep 50", "Rep 26", "Rep 43", 
"Rep 48", "Rep 70", "Rep 52", "Rep 33", "Rep 32", "Rep 32", "Rep 63", 
"Rep 45", "Rep 65", "Rep 12", "Rep 27", "Rep 63", "Rep 2", "Rep 3", 
"Rep 48", "Rep 37", "Rep 62", "Rep 59", "Rep 18", "Rep 44", "Rep 60", 
"Rep 41", "Rep 69", "Rep 11", "Rep 18", "Rep 60", "Rep 2", "Rep 26", 
"Rep 52", "Rep 57", "Rep 66", "Rep 65", "Rep 55", "Rep 1", "Rep 9", 
"Rep 1", "Rep 49", "Rep 39", "Rep 30", "Rep 49", "Rep 20", "Rep 27", 
"Rep 6", "Rep 37", "Rep 56", "Rep 3", "Rep 45", "Rep 68", "Rep 28", 
"Rep 51", "Rep 3", "Rep 32", "Rep 69", "Rep 50", "Rep 26", "Rep 51", 
"Rep 27", "Rep 13", "Rep 57", "Rep 54", "Rep 53", "Rep 37", "Rep 5", 
"Rep 11", "Rep 36", "Rep 54", "Rep 7", "Rep 63"), Manager = c("Manager 2", 
"Manager 12", "Manager 4", "Manager 4", "Manager 15", "Manager 3", 
"Manager 7", "Manager 8", "Manager 1", "Manager 1", "Manager 13", 
"Manager 10", "Manager 4", "Manager 15", "Manager 11", "Manager 9", 
"Manager 3", "Manager 8", "Manager 13", "Manager 3", "Manager 12", 
"Manager 14", "Manager 8", "Manager 15", "Manager 15", "Manager 9", 
"Manager 13", "Manager 14", "Manager 12", "Manager 4", "Manager 5", 
"Manager 11", "Manager 6", "Manager 14", "Manager 14", "Manager 14", 
"Manager 14", "Manager 7", "Manager 8", "Manager 6", "Manager 2", 
"Manager 12", "Manager 9", "Manager 8", "Manager 14", "Manager 12", 
"Manager 6", "Manager 12", "Manager 13", "Manager 4", "Manager 15", 
"Manager 5", "Manager 5", "Manager 9", "Manager 13", "Manager 12", 
"Manager 8", "Manager 4", "Manager 6", "Manager 7", "Manager 4", 
"Manager 14", "Manager 12", "Manager 4", "Manager 9", "Manager 15", 
"Manager 10", "Manager 6", "Manager 8", "Manager 14", "Manager 12", 
"Manager 15", "Manager 14", "Manager 8", "Manager 15", "Manager 2", 
"Manager 11", "Manager 8", "Manager 6", "Manager 5", "Manager 2", 
"Manager 13", "Manager 6", "Manager 3", "Manager 3", "Manager 6", 
"Manager 13", "Manager 10", "Manager 9", "Manager 10", "Manager 5", 
"Manager 14", "Manager 10", "Manager 4", "Manager 4", "Manager 10", 
"Manager 3", "Manager 9", "Manager 9", "Manager 10"), Director = c("Director 4", 
"Director 4", "Director 1", "Director 1", "Director 2", "Director 3", 
"Director 4", "Director 4", "Director 4", "Director 5", "Director 4", 
"Director 3", "Director 3", "Director 2", "Director 2", "Director 4", 
"Director 3", "Director 4", "Director 2", "Director 1", "Director 5", 
"Director 2", "Director 4", "Director 3", "Director 4", "Director 1", 
"Director 4", "Director 3", "Director 3", "Director 1", "Director 4", 
"Director 3", "Director 1", "Director 1", "Director 2", "Director 4", 
"Director 3", "Director 1", "Director 1", "Director 3", "Director 3", 
"Director 1", "Director 4", "Director 1", "Director 1", "Director 4", 
"Director 3", "Director 5", "Director 5", "Director 2", "Director 5", 
"Director 1", "Director 5", "Director 5", "Director 1", "Director 4", 
"Director 2", "Director 1", "Director 3", "Director 1", "Director 5", 
"Director 1", "Director 5", "Director 1", "Director 1", "Director 5", 
"Director 2", "Director 4", "Director 1", "Director 2", "Director 1", 
"Director 1", "Director 1", "Director 3", "Director 4", "Director 2", 
"Director 1", "Director 2", "Director 2", "Director 3", "Director 5", 
"Director 1", "Director 1", "Director 2", "Director 2", "Director 2", 
"Director 5", "Director 5", "Director 5", "Director 1", "Director 4", 
"Director 5", "Director 3", "Director 1", "Director 3", "Director 3", 
"Director 5", "Director 3", "Director 4", "Director 1")), .Names = c("Amount_USD__c", 
"StageName", "CloseDate", "Rep", "Manager", "Director"), row.names = c(2952L, 
1540L, 325L, 3901L, 3389L, 3625L, 709L, 1603L, 2855L, 1240L, 
1074L, 3174L, 3481L, 2336L, 2369L, 1671L, 141L, 3689L, 3505L, 
2758L, 3004L, 2971L, 3385L, 185L, 3719L, 2035L, 172L, 3331L, 
2975L, 1210L, 1428L, 2011L, 188L, 2259L, 2609L, 889L, 3838L, 
3301L, 2467L, 2924L, 261L, 4045L, 1481L, 1684L, 3836L, 1166L, 
1373L, 2732L, 2555L, 1452L, 513L, 2633L, 3823L, 3604L, 3842L, 
2757L, 2199L, 511L, 2811L, 672L, 3794L, 1440L, 3375L, 3027L, 
2996L, 2374L, 2334L, 3415L, 858L, 288L, 1582L, 2301L, 198L, 3315L, 
2620L, 2420L, 1641L, 3397L, 2765L, 3392L, 2709L, 654L, 3810L, 
3571L, 795L, 3198L, 2287L, 2307L, 355L, 1820L, 358L, 3992L, 2885L, 
563L, 2280L, 3049L, 537L, 1274L, 899L, 3268L), class = "data.frame")

总结了根据reactive plotly

提供输入和分组数据的功能
summarize_opps <- function(opp_data, variable){
  opps_summary <- opp_data %>%
    mutate(week = floor_date(CloseDate, 'week'),
           Director = ifelse(is.na(Director), "Missing", Director)) %>%
    group_by_(as.name(variable), 'StageName', 'week') %>%
    summarise(Amount = sum(Amount_USD__c)) %>%
    ungroup()

  return(opps_summary)

}

test_summary的负责人:

test_summary <- summarize_opps(test_opps, "Director")

  Director   StageName    week       Amount
  <chr>      <chr>        <date>      <dbl>
1 Director 1 1. Discovery 2018-04-01  1740.
2 Director 1 1. Discovery 2018-04-08  3624.
3 Director 1 1. Discovery 2018-04-15  1536.
4 Director 1 1. Discovery 2018-04-22   336.
5 Director 1 1. Discovery 2018-04-29  2388.
6 Director 1 1. Discovery 2018-05-06  3255.

检查结果:

ggplot(test_summary, aes(x = week, y = Amount, fill = StageName)) +
      geom_bar(stat = 'identity', color='black') +
      scale_y_continuous(labels = dollar) +
      coord_cartesian(xlim = c(min_week, max_week), ylim = c(0, max_amount)) +
      facet_wrap(~ Director)

enter image description here

闪亮版

# set min and max variables for slider
min_week <- min(test_summary[test_summary$week == floor_date(today(), 'week'),]$week)
max_week <- max(test_summary[test_summary$week < ceiling_date(today(), 'quarter'),]$week)
max_amount <- max(test_summary$Amount)

ui <- shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(width = 2,
                   sliderInput('of_time', "Time", min = min_week, max = max_week, value = c(min_week, max_week), step = 7),
                   selectInput('of_groupby', "Group By", c("Director", "Manager", "Rep"), selected = "Director")
                   ),
      mainPanel(
        tabsetPanel(
          tabPanel("Plot",
                   plotlyOutput("of_p"))
        )
      )
    )
  )
)

server <- function(input, output){
  plot_data <- reactive({
    opps_summary <- summarize_opps(test_opps, input$of_groupby)
    opps_summary <- opps_summary %>% filter(week >= input$of_time[1],
                                            week <= input$of_time[2])
    return(opps_summary)

  })

  output$of_p <- renderPlot({
    p6 <- ggplot(plot_data(), aes(x = week, y = Amount, fill = StageName)) +
      geom_bar(stat = 'identity', color='black') +
      scale_y_continuous(labels = dollar) +
      coord_cartesian(xlim = c(min_week, max_week), ylim = c(0, max_amount)) +
      facet_wrap(~ Director)



    ggplotly(p6) %>%
      layout(=TRUE)
  })
}

shinyApp(ui, server)

0 个答案:

没有答案