Control chart in R

I am working to create control chart in R, able to do it with qcc Library. I need to know the way of how to get the reason for a point that goes out of control.
Also I want to show chart with OOC and without OOC to end user.

Also please suggest me easy way to do a DPPM U chart in R, with axis specification

Could you maybe provide links for and/or images of what you're referring to? That way people without your area of knowledge might be able to help you with the code.

@ksasi2k3 - Your example has several points 'out of control'. The default for identifying rules is to apply Shewart's rules - see the documentation for the the function shewhart.rules The red points are beyond the limits. The yellow points are because a certain number of consecutive points fall on the same side of the mean. The default number is 7 specified by qcc.options("run.length").

A qcc object contains a violations component which contains the indices for the beyond.limits limits and violating.runs points. This will identify your reasons. Not sure what you mean by a chart without the OCC...

2 Likes

The chart without OOC means, I want to first identify the reason for OOC and then remove those red points from the chart and display the new chart which will show different variation. Sure I will try this shewhart.rules,meantime any example would help better as I am new to R

Best I can do without your code is to suggest you name your qcc object, i.e.

chrt <- qcc(....)

and then take a look at

chrt$violations

which will give you the indices for the OOC points - you can use those to remove them from your data set and re-plot.

Hi @ksasi2k3, to get further help, you'll need to provide a reproducible example. To learn how to make one, consider the thread:

3 Likes

sure will do that jrlewi .. meantime I want to attach excel which I cant. let me know how to do that, so that I will explain step by step about my problem

Hi Ksasi2k3 - if there is a way to attach an excel file...please don't. Generally, people are not going to download external files. Instead, the idea is to provide a smaller data set that demonstrates what you want to do directly in your R code - one way to do this is using dput. I suggest looking through the following discussion on stackoverflow:

2 Likes

We have a discussion here, too, with a whole boatload of ideas for sharing your data in a way that makes it easy for people to help you:

And no, the forum settings do not allow attaching Excel files, due to the obvious security risks.

2 Likes

Hi jrlewi, yeah I got it, I have already sent my entire problem as reply to REPREX.

My team does a lot of SPC charts in R, typically with {qicharts2}, but yeah, what you're describing is pretty advanced. We are actually extracting the data out of the ggplot2 object of "qic()" and have begun playing with representing things like special cause variation.

My initial thought would be to do some combination of qcc (to get rule violations + which points violate for your tagle) and qic to plot.

I've been confronting a similar problem at work, and if I tackle it there I'll come back and post it here :grinning:

yeah.. Christiannolan your thoughts are valid and let me know in future also if I solve this I will post. Mean time can you let me know to create U or P chart using my data with month labels in x axis.

To create a U chart, you need to specify a vector containing the number of defects observed for each group, the sample size of each group, and set type to 'u'. For example:

library(qcc)
data("orangejuice")
with(orangejuice,
     qcc(D, sizes = size, type = 'u')
)

To change the labels on the x axis, provide a vector to the labels argument:

library(lubridate)
date_labels <- strftime(ymd('2018-08-08') + days(0:53), '%m/%d') 
with(orangejuice,
     qcc(D, sizes = size, type = 'u' , labels = date_labels)
)

Hi, Thank you very much. I am able to create U chart using

library(qicharts2)
Uchart<-qic(Failed,
            n=Built,
            x=DateMonth,
            data=df1,
            # exclude = TRUE,
            # facets = NULL,
            show.labels = TRUE,
            multiply = 1000000,
            chart='u',
            title='Control chart before Analysis',
            xlab='Month',
            ylab='Defective parts per million',
          )
Uchart

Now I want to know how I can loop this using chart creation code.

Example: I have four columns Namely:

  1. Id
    2.Site
    3.Supplier
    4.Manufacturer

I want to loop through each column and create the chart. or for Individual column I need to run the chart code.

Please let me know for any suggestions.

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

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')
1 Like

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.

2 Likes

Thank you Galangjs, I am trying the above option and also exploring few other. Will keep you posted. Thank you once again for your effort. I really appreciate.

Hi [Galangjs], I am trying to save the R control chart and table from R to Powerpoint. Able to achieve the same with some restrictions.

library(RDCOMClient)
library(ggplot2)
TPPT<-PPT.Init(method = "RDCOMClient")
##Table
x1 <- (tbls2)
ggsave(plot=grid.table(tbls6),file=PlotOOC2.file,device = "png")
TPPT<-PPT.AddGraphicstoSlide(TPPT,file =PlotOOC2.file)
## To close
PPT.Close(myPres)
rm(myPres)

here I used "RDCOMClient" to Intialize PPT and used "ggsave" to save the table as PNG. Here I try to make the table as a plot by passing like "grid.table(tbls6)". I am able to get the table in PPT but first and last columns are displayed with half size. Also I am able to save Powerpoint(PPT) to local disk but unable to close it after executions.