Control chart in R


#21

It works for creating U chart and also labeling x axis. Thank you. I want to create DPPM, Defects per million. how can I add that in code. I want to add Multiply=1000000


#22

Well, I don't think there's an easy way to modify the y-axis labels in plots produced by qcc. As suggested by @Christiannolan, your best bet is to take the object produced by qcc, extract the relevant data from it, and then plot using ggplot. It's not the easiest thing to do, but if you're game to try, here's how I would approach it:

library(tidyverse)
library(qcc)
library(scales)

data("orangejuice")

u_chart_obj <- with(orangejuice, qcc(D, sizes = size, type = 'u'))

oj <- mutate(orangejuice, 
             row_id = 1:nrow(orangejuice),
             # identify rows of interest
             beyond_limits = row_id %in% u_chart_obj$violations$beyond.limits,
             violating_runs = row_id %in% u_chart_obj$violations$violating.runs,
             # data for plotting control limits
             center = u_chart_obj$center,
             lcl = u_chart_obj$limits[1, 'LCL'],
             ucl = u_chart_obj$limits[1, 'UCL'])

update_geom_defaults('point', list(size = 3))

ggplot(oj, aes(sample, D/size)) + 
  # create a basic chart with dots and lines
  geom_line(color = 'gray', size = 0.5) +
  geom_point() + 
  # mark certain points red or gold
  geom_point(color = 'red', data = filter(oj, beyond_limits)) +
  geom_point(color = 'gold', data = filter(oj, violating_runs)) +
  # add the control limits and center line
  geom_hline(aes(yintercept = lcl), linetype = 'dashed') +
  geom_hline(aes(yintercept = ucl), linetype = 'dashed') +
  geom_hline(aes(yintercept = center)) +
  # express y axis labels in 'per million'
  scale_y_continuous(labels = unit_format(scale = 1e6, unit = '')) +
  # misc cleanup
  theme_light() +
  labs(x = 'Sample Number', y = 'Defects per million units')

#23

Thank you, yeah I tried it for my data but got error as follows;
Error:
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Column x must be a 1d atomic vector or a list

here is the code I am using

library(tidyverse)
library(qcc)
library(scales)
#my data is currTableDF
print(currTableDF)

#columns in my data ID, Group,Month, supplier, Built, Failed
u_chart_obj <- with(currTableDF, qcc(Failed, sizes = Built, type = 'u'))

oj <- mutate(currTableDF, 
             row_id = 1:nrow(currTableDF),
             # identify rows of interest
             beyond_limits = row_id %in% u_chart_obj$violations$beyond.limits,
             violating_runs = row_id %in% u_chart_obj$violations$violating.runs,
             # data for plotting control limits
             center = u_chart_obj$center,
             lcl = u_chart_obj$limits[1, 'LCL'],
             ucl = u_chart_obj$limits[1, 'UCL'])

update_geom_defaults('point', list(size = oj$Built))

ggplot(oj, aes(sample, Failed/Built)) + 
  # create a basic chart with dots and lines
  geom_line(color = 'gray', size = 0.5) +
  geom_point() + 
  # mark certain points red or gold
  geom_point(color = 'red', data = filter(oj, beyond_limits)) +
  geom_point(color = 'gold', data = filter(oj, violating_runs)) +
  # add the control limits and center line
  geom_hline(aes(yintercept = lcl), linetype = 'dashed') +
  geom_hline(aes(yintercept = ucl), linetype = 'dashed') +
  geom_hline(aes(yintercept = center)) +
  # express y axis labels in 'per million'
  scale_y_continuous(labels = unit_format(scale = 1e6, unit = '')) +
  # misc cleanup
  theme_light() + 
  labs(x = 'Sample Number', y = 'Defects per million units')

sample data:

Id Group Month Supplier Built Failed
85891 Top1 201801 Tato 94376 70
85891 Top1 201802 Tato 26434 64
85891 Top1 201803 Tato 31767 91
85891 Top1 201804 Tato 61980 173
85891 Top1 201805 Tato 56220 183
85891 Top1 201806 Tato 67935 148
85891 Top1 201807 Tato 60351 60
85891 Top1 201808 Tato 20527 22

Also I want know how "Month" can be shown in x axis and if possible please let me know about phase and annotation.


#24

My apologies. I should have added a comment before update_geom_defaults(...). This just sets the default size of the points on the plot and has nothing to do with the size of the samples being tested. Leaving that line as

update_geom_defaults('point', list(size = 3))

should remove the error.

Another thing I should have done was explicitly name the arguments to aes. This function tells ggplot which columns map to which parts of the plot. It would have been better to write

ggplot(oj, aes(x = sample, y = D/size)) +

This makes it a little obvious that the column sample should be used to determine the x coordinate and the quantity Failed/Built should be used to determine the y coordinate. In your case, you just need to replace sample with Month.

For annotation, I'll refer you to the documentation for the function annotate.

For phasing, there's a couple of approaches that come to mind. One possibility is to use group argument in aes. Another possibility is to use the function facet_wrap.