This document will present some basics of how to create x-bar and R charts such as those that could be used in a phase I evaluation of a process. Before using this tutorial, you will need to install R, preferably using the RStudio interface. You can download RStudio for desktop at https://www.rstudio.com/products/rstudio/.
There are several packages that make control charts easily. This tutorial will use the qcc package, but the SixSigma also has a method to do the same thing and it is fairly easy to make these charts manually using qqplot2 or just a simple plot.
We’ll start with a simple x-bar plot where n = 1, and then we’ll learn how to plot charts where n > 1.
First we need to input the data. We do this as shown below where the data is listed as a column in a table.
chartdata <- read.table(header = FALSE, text = "
0.731
0.734
0.733
0.734
0.733
0.734
0.734
0.733
0.734
0.715
0.734
0.732
0.735
0.734
0.734
0.731
0.731
0.735
0.713
0.731
0.715
0.736
0.733
0.733
0.732
0.732
0.733
0.715
0.734
0.734
")
Making a plot from the data is quite simple. First we’ll need to call the qcc package, and if this is the first time we’ve used it we’ll need to install it. (To install it in RStudio go to the “Tools” menu, select “Install Packages…” and type “qcc” into the packages field being sure to also select “Install Dependencies” and click “Install.” if you are not using RStudio, you can type “install.packages(”qcc“)” into the R console.)
library(qcc)
Once we’ve called the qcc library, we can use qcc to make an x-bar chart of the data as follows. Simply call the data set, the type of chart to generate, and whether to display a plot of the chart as shown below.
qcc(data = chartdata, # The dataset
type = "xbar.one", # The chart type (in this case it lets qcc know that n = 1)
plot = TRUE) # Display the plot
## List of 11
## $ call : language qcc(data = chartdata, type = "xbar.one", plot = TRUE)
## $ type : chr "xbar.one"
## $ data.name : chr "chartdata"
## $ data : num [1:30, 1] 0.731 0.734 0.733 0.734 0.733 0.734 0.734 0.733 0.734 0.715 ...
## ..- attr(*, "dimnames")=List of 2
## $ statistics: Named num [1:30] 0.731 0.734 0.733 0.734 0.733 0.734 0.734 0.733 0.734 0.715 ...
## ..- attr(*, "names")= chr [1:30] "1" "2" "3" "4" ...
## $ sizes : int [1:30] 1 1 1 1 1 1 1 1 1 1 ...
## $ center : num 0.731
## $ std.dev : num 0.00547
## $ nsigmas : num 3
## $ limits : num [1, 1:2] 0.714 0.747
## ..- attr(*, "dimnames")=List of 2
## $ violations:List of 2
## - attr(*, "class")= chr "qcc"
As you can see, qcc generated a chart as well as provided information about the chart including the control limits, mean and standard deviation, the number of Shewhart rule violations for both runs and limits, and it color codes the violations.
Now we will plot an x-bar chart where n > 1. First we will read in the data. We will again do this using read.table but this time we will have 25 samples of n = 5 where each observation is contained in a different column.
chartdata2 <- read.table(header = FALSE, text = "
0.07705 0.076030001 0.076959997 0.075489998 0.076920003
0.078160003 0.077119999 0.076250002 0.076690003 0.076470003
0.077550001 0.077079996 0.077129997 0.076839998 0.077440001
0.07581 0.07728 0.076549999 0.077760004 0.077540003
0.077459998 0.079109997 0.077830002 0.07711 0.078670003
0.0766 0.078039996 0.076240003 0.077660002 0.075900003
0.077919997 0.074440002 0.078419998 0.077370003 0.074500002
0.077299997 0.076370001 0.075539999 0.076800004 0.078280002
0.075460002 0.076229997 0.077959999 0.07491 0.078620002
0.07931 0.077420004 0.07519 0.07784 0.07649
0.076609999 0.076860003 0.077720001 0.075790003 0.078380004
0.078050002 0.075450003 0.078400001 0.076810002 0.077770002
0.077289999 0.078170002 0.077260002 0.076849997 0.075120002
0.077639997 0.076820001 0.077200003 0.079180002 0.078299999
0.077610001 0.075630002 0.079350002 0.076920003 0.075800002
0.076509997 0.076970004 0.077919997 0.078189999 0.077660002
0.07863 0.077200003 0.077310003 0.076530002 0.075680003
0.075730003 0.07807 0.075580001 0.074589998 0.076499999
0.076849997 0.076140001 0.077370003 0.075790003 0.07705
0.076370001 0.076700002 0.075620003 0.077399999 0.076559998
0.076180004 0.078189999 0.07626 0.076580003 0.077459998
0.076059997 0.075350001 0.077310003 0.07553 0.077990003
0.076889999 0.075240001 0.076609999 0.077090003 0.079120003
0.07762 0.078919999 0.078730002 0.076849997 0.077660002
0.078299999 0.075479999 0.076619998 0.078780003 0.07728")
Sometimes it is not this easy to read in the data, especially if the set is very large. Another easy way to read it in, if you are using RStudio, is to use the “Import Dataset” button in the top right box of the default layout and import your data from a .csv file.
Now we will create an x-bar chart of this data. We will again use a qcc object and this time we will customize the plot with a new title and limit the number of significant digits displayed below the chart.
xbar_chart2 <- qcc(data = chartdata2,
type = "xbar",
sizes = 5,
title = "Sample X-bar Chart Title", # Replacement title
digits = 2, # Limit the signifciant figures
plot = TRUE)
You’ll notice that this time I saved the qcc object as xbar_chart2 so that I could refer back to it and that because I did this it didn’t display all of the information that running qcc without saving it did. To see all of that extra stuff, call the object by itself like this:
xbar_chart2
## List of 11
## $ call : language qcc(data = chartdata2, type = "xbar", sizes = 5, plot = TRUE, title = "Sample X-bar Chart Title", digits = 2)
## $ type : chr "xbar"
## $ data.name : chr "chartdata2"
## $ data : num [1:25, 1:5] 0.077 0.0782 0.0776 0.0758 0.0775 ...
## ..- attr(*, "dimnames")=List of 2
## $ statistics: Named num [1:25] 0.0765 0.0769 0.0772 0.077 0.078 ...
## ..- attr(*, "names")= chr [1:25] "1" "2" "3" "4" ...
## $ sizes : num [1:25] 5 5 5 5 5 5 5 5 5 5 ...
## $ center : num 0.077
## $ std.dev : num 0.00112
## $ nsigmas : num 3
## $ limits : num [1, 1:2] 0.0755 0.0785
## ..- attr(*, "dimnames")=List of 2
## $ violations:List of 2
## - attr(*, "class")= chr "qcc"
We can improve the plot visually by customizing the colors using the following code to set the background to grey and the margins to white.
qcc.options(bg.margin = "white", bg.figure = "gray95")
Then the plot will look like this:
Finally, to create an r chart we repeat the process above except now we will specify that we want an r chart instead of an x-bar chart. Note that the color scheme remains altered since we changed qcc.options above.
xbar_chart2 <- qcc(data = chartdata2,
type = "R",
sizes = 5,
title = "Sample R Chart Title",
digits = 2,
plot = TRUE)
This has been a very limited introduction to plotting control charts using the qcc package. This package is also capable of doing things like calculating the process capability (Cp, Cpk, etc), creating time weighted control charts (CUSUM and EWMA), other Shewhart charts (c, n, etc), creating OC curves, and even making cause and effect diagrams. For more information on the qcc package, read the documentation found at https://cran.r-project.org/web/packages/qcc/.
Finally, Tom Hopper figured out how to make qcc plots using the ggplot2 and grid packages to make them more visually pleasing. Learn how to use his implementation at https://tomhopper.me/2014/03/03/rewriting-plot-qcc-using-ggplot2-and-grid/.