Annual performance comparison

We calculate how many dollars are lost per unit volume (\(in^3\)) by taking our total reject cost (based on our subset of {PSZT/FBG, not3d, noEC, only BE}), and dividing by total fired volume (total volume of the subset fired).

Additional explanation in next section.

Costs per volume

# *costs per volume ----
g.title =    "Costs per volume vs month 2019-20"
g.subtitle = "CPV = Reject costs / Fired volume"
g.caption =  "From merged dataset"

df_recent_costs_per_volume <- df_merged %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG") &
                  cause == "BE" &
                  year(fire_date) >= 2019) %>%
  mutate(month = month(fire_date,label=TRUE)) %>%
  group_by(year,year_month,month) %>% 
  dplyr::summarise(total_reject_cost = sum(cost_of_reject_single_row),
                   total_fired_vol = sum(total_item_fired_vol)) %>% 
  mutate(cost_per_volume = total_reject_cost/total_fired_vol)

gg_recent_costs_per_volume <- 
  df_recent_costs_per_volume %>% 
  ggplot(aes(x=month,
             text=paste("Year:",year,
                        "<br>Total reject cost:",mycurrency(total_reject_cost),
                        "<br>Total fired volume:",mynumber(total_fired_vol),
                        "<br>Cost per volume:",mycurrency(cost_per_volume)
                        )
             ))+
  geom_bar(aes(y=cost_per_volume,
               fill=year),
           position="dodge",
           stat="identity")+
  theme_minimal()+
  labs(title = g.title,
     subtitle = g.subtitle,
     caption = g.caption)+
  scale_fill_brewer(palette = "Paired")+
  scale_y_continuous(name="Cost per volume",
                     labels = dollar_format(accuracy=.005),
                     breaks=seq(0,.03,.005))+
  xlab("Month")

gp <- ggplotly(gg_recent_costs_per_volume, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Savings / Loss

Bar

# *savings-loss ---- 
g.title =    "CPV vs month, 2020, with estimated savings"
g.subtitle = "S/L = (Current fired volume * Last years CPV) - Current reject cost"
g.caption =  "From merged dataset"

df_recent_costs_per_volume_sub <- 
  df_recent_costs_per_volume[summary(df_recent_costs_per_volume$month)>1,]

df_recent_costs_per_volume_sub_2019 <- df_recent_costs_per_volume_sub %>% 
  dplyr::filter(year == 2019) %>%
  set_colnames(c(
    "year",
    "year_month",
    "month",
    "total_reject_cost_2019",
    "total_fired_vol_2019",
    "cost_per_volume_2019"))

df_recent_costs_per_volume_sub_2019 <- df_recent_costs_per_volume_sub_2019 %>% 
  ungroup() %>% 
  dplyr::select(month,total_reject_cost_2019,total_fired_vol_2019,cost_per_volume_2019)

df_recent_costs_per_volume_sub <- df_recent_costs_per_volume_sub %>% 
  left_join(df_recent_costs_per_volume_sub_2019) %>%
  mutate(savings_loss = 
           ifelse(year==2020,
                  (total_fired_vol*cost_per_volume_2019)-total_reject_cost,
                  NA))

# get max CPV value for geom_text
max_geom_text <- df_recent_costs_per_volume_sub %>% 
  ungroup() %>% 
  dplyr::filter(!is.na(savings_loss)) %>% 
  dplyr::select(cost_per_volume) %>% 
  unlist() %>% 
  max() %>% 
  signif(1)

gg_recent_costs_per_volume_sub <- df_recent_costs_per_volume_sub %>% 
    ggplot(aes(x=month,
             fill=year,
             text=paste("Year:",year,
                        "<br>Total reject cost:",mycurrency(total_reject_cost),
                        "<br>Total fired volume:",mynumber(total_fired_vol),
                        "<br>Cost per volume:",mycurrency(cost_per_volume),
                        "<br>Savings/loss:",mycurrency(savings_loss)
             )
             
             ))+
  geom_bar(stat="identity",aes(y=cost_per_volume),
           position='dodge')+
  geom_text(data=df_recent_costs_per_volume_sub[df_recent_costs_per_volume_sub$year!=2019,],
              aes(label=mycurrency(savings_loss), 
                # fill=year,
                y=max_geom_text))+
                # y=cost_per_volume+.001))+
  theme_minimal()+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  scale_fill_brewer(palette = "Paired")+
  scale_y_continuous(name="Cost per volume",
                     labels = dollar_format(accuracy=.005),
                     breaks=seq(0,.03,.005))+
  xlab("Month")

gp <- ggplotly(gg_recent_costs_per_volume_sub, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Table

Explanation

To illustrate how the cost per volume metric is calculated, see the below, semi-filtered table. The only difference between this table and the subset we have been working with in above calculations, is the inclusion of all defects, as opposed to only the bad edge defects (in bold).

Take note of the blue rows. This item, UDICELL 100X100X25 10PPI PSZT, had several defects. The total volume of this item fired is found in the last column, total_item_fired_vol. The reject volume for each defect type can be found in the column, reject_vol_single_row, along with the accompanying cost associated with each rows loss, cost_of_reject_single_row.

The reported measure of cost per volume is calculated by taking only the bad edges rows, taking the sums of both columns–cost_of_reject_single_row, and reject_vol_single_row–and dividing them, resulting in a cost per volume for each time period

This is basically an inverse yield calculation based on dollars. The lower the number means we are losing less dollars per unit volume fired. When comparing months or years together, the month or year with a lower value is performing better. To attach a dollar value we simply take the previous years cost per volume and multiply by our current volume fired. We then subtract the dollar values to determine the difference in dollars lost.

Cost per Item

It may be useful to also look at any savings or loss from the a per item basis as well. The issue with using volumes may be that estimates tend to be higher.

Previous years

Various measures of historical defect data in terms of dollars lost.

BE only

Only bad edges, subset to only PSZT/FBG, noEC, not3d.

vs Other causes

Same as before, but the subset is now bad edges. In other words, comparing bad edges to all other defects.

Line

g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Comparing BE to all other defects"
g.caption =  "From defects dataset"

gg_be_subset_costs <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG")) %>%
  dplyr::mutate(isBadEdge = ifelse(cause == "BE", "Bad edge", "All others")) %>%
  group_by(year,isBadEdge) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                            ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
              ) %>% 
  ggplot(aes(x=year,
             fill=isBadEdge,
             text = paste("Subset:", isBadEdge,
                          "<br>Subset cost:", mycurrency(total_cost_cause),
                          "<br>Stacked cost (sum of both):", mycurrency(total_cost_all_causes))
  ))+
  # geom_bar(stat="identity", aes(y=total_cost_cause),
  #          # position="dodge",
  #          color="royalblue")+
  geom_point(aes(y=total_cost_cause,color=isBadEdge))+
  geom_line(aes(y=total_cost_cause,group=1,color=isBadEdge))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  # scale_fill_brewer(name="Subset",
  #                   palette="Blues")+
  scale_color_brewer(name="",
                     palette="Set1")+
  xlab("Year")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))

gp <- ggplotly(gg_be_subset_costs, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Dodged

# *subset by BE vs notBE, of subset----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Comparing BE to all other defects"
g.caption =  "From defects dataset"

gg_be_subset_costs <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG")) %>%
  dplyr::mutate(isBadEdge = ifelse(cause == "BE", "Bad edge", "All others")) %>%
  group_by(year,isBadEdge) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                            ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
              ) %>% 
  ggplot(aes(x=year,
             fill=isBadEdge,
             text = paste("Subset:", isBadEdge,
                          "<br>Subset cost:", mycurrency(total_cost_cause),
                          "<br>Stacked cost (sum of both):", mycurrency(total_cost_all_causes))
  ))+
  geom_bar(stat="identity", aes(y=total_cost_cause),
           position="dodge",
           color="royalblue")+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  scale_fill_brewer(name="Subset",
                    palette="Blues")+
  scale_color_brewer(name="",
                     palette="Set1")+
  xlab("Year")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))

gp <- ggplotly(gg_be_subset_costs, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Stacked

# *subset by BE vs notBE, of subset----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Comparing BE to all other defects"
g.caption =  "From defects dataset"

gg_be_subset_costs <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG")) %>%
  dplyr::mutate(isBadEdge = ifelse(cause == "BE", "Bad edge", "All others")) %>%
  group_by(year,isBadEdge) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                            ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
              ) %>% 
  ggplot(aes(x=year,
             fill=isBadEdge,
             text = paste("Subset:", isBadEdge,
                          "<br>Subset cost:", mycurrency(total_cost_cause),
                          "<br>Stacked cost (sum of both):", mycurrency(total_cost_all_causes))
  ))+
  geom_bar(stat="identity", aes(y=total_cost_cause),
           # position="dodge",
           color="royalblue")+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  scale_fill_brewer(name="Subset",
                    palette="Blues")+
  scale_color_brewer(name="",
                     palette="Set1")+
  xlab("Year")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))

gp <- ggplotly(gg_be_subset_costs, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

vs All causes

Expanding “all other defects” into each category of defect.

Line

df_be_subset_costs <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG")) %>%
  group_by(year,cause) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                            ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
              ) %>% 
  mutate(proportion = total_cost_cause/total_cost_all_causes)

df_cause_sort <- df_be_subset_costs %>% 
  group_by(cause) %>% 
  dplyr::summarise(total_cost_all_causes = sum(total_cost_cause)) %>% 
  arrange(-total_cost_all_causes)

df_be_subset_costs$cause <- factor(df_be_subset_costs$cause, levels=paste(df_cause_sort$cause))

gg_be_subset_costs <- df_be_subset_costs %>% 
  ggplot(aes(x=year,
             # fill=forcats::fct_rev(cause),
             # fill=cause,
             text = paste("Cause:", cause,
                          "<br>Cost of defect:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion)
                          )
  ))+
  # geom_bar(stat="identity", aes(y=total_cost_cause),
  #          # position="dodge",
  #          color="grey70")+
  geom_point(aes(y=total_cost_cause,color=cause))+
  geom_line(aes(y=total_cost_cause,group=cause,color=cause))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  scale_color_viridis_d(option="E",direction = -1,name="Cause")
  # scale_fill_viridis_d(option="A",direction = 1,name="Cause")
  # scale_fill_viridis_d(option="A",direction = -1,name="Cause")

gp <- ggplotly(gg_be_subset_costs, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Dodged

Stacked

by Proportion

To make comparison between years of different values easier, values are converted to proportions of entire annual costs.

Line

g.title =    "Proportion of costs of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Subset by all causes"
g.caption =  "From defects dataset"

gg_be_subset_costs_prop <- df_be_subset_costs %>%
  ggplot(aes(x=year,
             # fill=forcats::fct_rev(cause),
             # fill=cause,
             text = paste("Cause:", cause,
                          "<br>Cost of cause:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion))
             ))+
  # geom_bar(stat="identity", aes(y=proportion),
  #          position="dodge",
  #          color="grey70")+
  geom_point(aes(y=proportion,color=cause))+
  geom_line(aes(y=proportion,color=cause,group=cause))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year")+
  scale_y_continuous(name="Distribution of costs",
                     label = percent_format(accuracy=1),
                     breaks = seq(0,1,.1))+
  scale_color_viridis_d(option="D",direction = -1,name="Cause")
  # scale_fill_viridis_d(option="A",direction = -1,name="Cause")
  # scale_fill_viridis_d(option="A",direction = 1,name="Cause")

gp <- ggplotly(gg_be_subset_costs_prop, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.08, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Dodged

Stacked

Recent

Same as above measures, expanded by year-month since 2019.

BE only

Only bad edges, subset to only PSZT/FBG, noEC, not3d.

# *bad edges only, of subset ----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC, BE} rejected parts"
g.subtitle = "Total cost of bad edges in the subset"
g.caption =  "From defects dataset"

gg_be_costs_recent <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG" | 
                     composition == "PSZT FBG") & 
                  cause == "BE" & 
                  year(fire_date) >= 2019) %>% 
  group_by(year, year_month) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  ggplot(aes(x=year_month,
             text = paste("Year-month:" , year_month,
                          "<br>Total cost:", mycurrency(total_cost_cause))
  ))+
  geom_bar(stat="identity", aes(y=total_cost_cause),fill="deepskyblue2")+
  geom_point(aes(y=total_cost_cause))+
  geom_line(aes(y=total_cost_cause,group=1))+
  theme_minimal()+
  theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year-month")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))

gp <- ggplotly(gg_be_costs_recent, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

vs Other causes

Same as before, but the subset is now bad edges. In other words, comparing bad edges to all other defects.

Line

# *subset by BE vs notBE, of subset----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Comparing BE to all other defects"
g.caption =  "From defects dataset"

gg_be_subset_costs_recent <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG" | 
                     composition == "PSZT FBG") & 
                  year(fire_date) >= 2019) %>% 
  dplyr::mutate(isBadEdge = ifelse(cause == "BE", "Bad edge", "Other causes")) %>%
  group_by(year, year_month, isBadEdge) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                              ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year_month) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
  ) %>% 
  ggplot(aes(x=year_month,
             fill=forcats::fct_rev(isBadEdge),
             text = paste("Year-month:", year_month,
                          "<br>Subset:", isBadEdge,
                          "<br>Subset cost:", mycurrency(total_cost_cause),
                          "<br>Stacked cost (sum of both):", mycurrency(total_cost_all_causes))
  ))+
  # geom_bar(stat="identity", aes(y=total_cost_cause),
  #          # position="dodge",
  #          color="grey70",
  #          width=.8)+
  geom_line(aes(y=total_cost_cause,
                group=isBadEdge,
                color=forcats::fct_rev(isBadEdge)))+
  geom_point(aes(y=total_cost_cause,
                 color=forcats::fct_rev(isBadEdge)))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  # scale_fill_brewer(name="Subset",
  #                   palette="Set3")+
  scale_fill_brewer(name="Subset",
                    palette="Set1")+
  scale_color_brewer(name="",
                     palette="Set1")+
  xlab("Year-month")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))


gp <- ggplotly(gg_be_subset_costs_recent, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Stacked

# *subset by BE vs notBE, of subset----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Comparing BE to all other defects"
g.caption =  "From defects dataset"

gg_be_subset_costs_recent <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG" | 
                     composition == "PSZT FBG") & 
                  year(fire_date) >= 2019) %>% 
  dplyr::mutate(isBadEdge = ifelse(cause == "BE", "Bad edge", "Other causes")) %>%
  group_by(year, year_month, isBadEdge) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                              ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year_month) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
  ) %>% 
  ggplot(aes(x=year_month,
             fill=forcats::fct_rev(isBadEdge),
             text = paste("Subset:", isBadEdge,
                          "<br>Subset cost:", mycurrency(total_cost_cause),
                          "<br>Stacked cost (sum of both):", mycurrency(total_cost_all_causes))
  ))+
  geom_bar(stat="identity", aes(y=total_cost_cause),
           # position="dodge",
           color="grey70",
           width=.8)+
  # geom_line(aes(y=total_cost_cause,
  #               group=isBadEdge,
  #               color=forcats::fct_rev(isBadEdge)))+
  # geom_point(aes(y=total_cost_cause,
  #                color=forcats::fct_rev(isBadEdge)))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  scale_fill_brewer(name="Subset",
                    palette="Set3")+
  scale_color_brewer(name="",
                     palette="Set1")+
  xlab("Year-month")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))


gp <- ggplotly(gg_be_subset_costs_recent, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

vs All causes

Expanding “all other defects” into each category of defect.

Line

# *subset by all causes, of subset----
g.title =    "Cost of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Subset by all causes"
g.caption =  "From defects dataset"

df_be_subset_costs_recent <- df_defects %>% 
  dplyr::filter(ppi !='3d' &
                  ec == "None" &
                  (composition == "PSZT" | 
                     composition == "PSZT-FBG") &
                year(fire_date) >= 2019) %>%
  group_by(year_month,cause) %>% 
  dplyr::summarise(total_cost_cause = sum(cost_of_reject_single_row)) %>% 
  left_join(df_defects %>% 
              dplyr::filter(ppi !='3d' &
                              ec == "None" &
                              (composition == "PSZT" | 
                                 composition == "PSZT-FBG")) %>%
              group_by(year_month) %>% 
              dplyr::summarise(total_cost_all_causes = sum(cost_of_reject_single_row))
  ) %>% 
  mutate(proportion = total_cost_cause/total_cost_all_causes)

df_cause_sort <- df_be_subset_costs_recent %>% 
  group_by(cause) %>% 
  dplyr::summarise(total_cost_all_causes = sum(total_cost_cause)) %>% 
  arrange(-total_cost_all_causes)

df_be_subset_costs_recent$cause <- factor(df_be_subset_costs_recent$cause, 
                                          levels=paste(df_cause_sort$cause))

gg_be_subset_costs_recent <- df_be_subset_costs_recent %>% 
  ggplot(aes(x=year_month,
             # fill=forcats::fct_rev(cause),
             fill=cause,
             text = paste("Year-month", year_month,
                          "<br>Cause:", cause,
                          "<br>Cost of defect:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion)
             )
  ))+
  # geom_bar(stat="identity", aes(y=total_cost_cause),
  #          # position="dodge",
  #          color="grey70")+
  geom_line(aes(y=total_cost_cause,
                group=cause,
                color=cause))+
                # color=forcats::fct_rev(cause)))+
  geom_point(aes(y=total_cost_cause,
                 # color=cause))+
                 color=forcats::fct_rev(cause)))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year-month")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))+
  scale_color_viridis_d(name="Cause", direction=-1,option="E")+
  scale_fill_viridis_d(name="Cause",  direction=-1,option="E")

gp <- ggplotly(gg_be_subset_costs_recent, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Stacked

gg_be_subset_costs_recent <- df_be_subset_costs_recent %>% 
  ggplot(aes(x=year_month,
             fill=forcats::fct_rev(cause),
             # fill=cause,
             text = paste("Year-month", year_month,
                          "<br>Cause:", cause,
                          "<br>Cost of defect:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion)
             )
  ))+
  geom_bar(stat="identity", aes(y=total_cost_cause),
           # position="dodge",
           color="grey70")+
  # geom_line(aes(y=total_cost_cause,
  #               group=cause,
  #               color=cause))+
  #               # color=forcats::fct_rev(cause)))+
  # geom_point(aes(y=total_cost_cause,
  #                # color=cause))+
  #                color=forcats::fct_rev(cause)))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year-month")+
  ylab("Total cost (USD)")+
  scale_y_continuous(label = dollar_format(suffix="K", scale= 1e-3 ))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))+
  scale_fill_viridis_d(name="Cause",  direction=1,option="D")
  # scale_color_viridis_d(name="Cause", direction=-1,option="E")+
  # scale_fill_viridis_d(name="Cause",  direction=-1,option="E")

gp <- ggplotly(gg_be_subset_costs_recent, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

by Proportion

Line

# *subset by all causes, proportions, of subset----
g.title =    "Proportion of costs of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Subset by all causes"
g.caption =  "From defects dataset"

gg_be_subset_costs_recent_prop <- df_be_subset_costs_recent %>%
  ggplot(aes(x=year_month,
             # fill=forcats::fct_rev(cause),
             fill=cause,
             text = paste("Year-month:", year_month,
                          "<br>Cause:", cause,
                          "<br>Cost of cause:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion))
  ))+
  # geom_bar(stat="identity", aes(y=proportion),
  #          # position="dodge",
  #          color="grey70")+
  geom_line(aes(y=proportion,
                group=cause,
                color=cause))+
                # color=forcats::fct_rev(cause)))+
  geom_point(aes(y=proportion,
                 # color=cause))+
                 color=forcats::fct_rev(cause)))+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year-month")+
  scale_y_continuous(name="Distribution of costs",
                     label = percent_format(accuracy=1),
                     breaks = seq(0,1,.1))+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))+
  scale_color_viridis_d(name="Cause", direction=-1,option="E")+
  scale_fill_viridis_d(name="Cause",  direction=-1,option="E")
  # scale_fill_viridis_d(option="A",direction = 1,name="Cause")+

gp <- ggplotly(gg_be_subset_costs_recent_prop, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp

Stacked

# *subset by all causes, proportions, of subset----
g.title =    "Proportion of costs of {not 3d, PSZT/FBG, noEC} rejected parts"
g.subtitle = "Subset by all causes"
g.caption =  "From defects dataset"

gg_be_subset_costs_recent_prop <- df_be_subset_costs_recent %>%
  ggplot(aes(x=year_month,
             fill=forcats::fct_rev(cause),
             # fill=cause,
             text = paste("Year-month:", year_month,
                          "<br>Cause:", cause,
                          "<br>Cost of cause:", mycurrency(total_cost_cause),
                          "<br>Cost of all defects:", mycurrency(total_cost_all_causes),
                          "<br>Percentage of whole:", mypercent(proportion))
  ))+
  geom_bar(stat="identity", aes(y=proportion),
           # position="dodge",
           color="grey70")+
  theme_minimal()+
  # theme(legend.position = "none")+
  labs(title = g.title,
       subtitle = g.subtitle,
       caption = g.caption)+
  xlab("Year")+
  scale_y_continuous(name="Distribution of costs",
                     label = percent_format(accuracy=1),
                     breaks = seq(0,1,.1))+
  scale_fill_viridis_d(option="A",direction = 1,name="Cause")+
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1,size=8))
  # scale_fill_viridis_d(option="A",direction = -1,name="Cause")

gp <- ggplotly(gg_be_subset_costs_recent_prop, tooltip = c("text"))%>%
  layout(title = list(text = paste0(g.title,
                                    '<br>',
                                    '<sup>',
                                    g.subtitle,
                                    '</sup>')),
         margin=list(t = 75),
         annotations = 
           list(x = 1, y = -0.14, 
                text = g.caption, 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="black"))
  )
# gp <- layout(gp, margin=list(t = 75))
# gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.06
# gp[['x']][['layout']][['annotations']][[1]][['y']] <- -0.1
gp