<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/ggqc-ggplot-quality-control-charts-new-release/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-bar.net/ggqc-ggplot-quality-control-charts/"> R – R-BAR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="ggQC Release 0.0.31" src="https://i2.wp.com/cdn.r-bar.net/PID627/v0p0p31-c.gif?w=456" data-recalc-dims="1">The ggQC package is a quality control extension for ggplot. Use it to create XmR, XbarR, C and many other highly customizable <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/#ControlCharts" target="_blank">Control Charts</a>. Additional statistical process control functions include Shewart <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/#violation" target="_blank">violation</a> checks as well as <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/#capability" target="_blank">capability analysis</a>. If your process is running smoothly, visualize the potential impacted of your next process improvement with a <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/#pareto" target="_blank">Pareto</a> chart. To learn more, read on!</p>
<p>To get started with ggQC, install it from CRAN by running the following code:</p>
<div><table><tr><td><pre>1
</pre></td><td><pre><span>install.<span>packages</span></span><span>(</span><span>&quot;ggQC&quot;</span><span>)</span></pre></td></tr></table></div>
<h3>ggQC Control Charts</h3>
<p>Control charts are a great way to monitor process outputs, drive improvement, and evaluate measurement systems. The types of control chart types supported by ggQC include:</p>
<ul>
<li><strong>Individuals Charts</strong> : <a rel="nofollow" href="http://rcontrolcharts.com/XmR_HOWTO.html" target="_blank">mR, XmR</a></li>
<li><strong>Attribute Charts</strong> : <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/" target="_blank">c, np, p, u </a></li>
<li><strong>Studentized Charts</strong>: <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/" target="_blank">xBar.rBar, xBar.rMedian, xBar.sBar, xMedian.rBar, xMedian.rMedian</a></li>
<li><strong>Dispersion Charts</strong>: rBar, rMedian, sBar</li>
</ul>
<p>The process for building control charts with ggQC is simple. First, load the ggQC and ggplot2 libraries. Next, load your data into R. Your data should be in long-form. The data set below provides an example of long form data if you're not familiar with the term. </p>
<p><img alt="Long Form Data Example" src="https://i2.wp.com/cdn.r-bar.net/PID627/long_form_data.png?w=456" data-recalc-dims="1"></p>
<p>Finally, make your control chart using standard ggplot layer-by-layer syntax and the stat_QC() command. The example code below, shows how all these steps come together to make an XmR plot. </p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td><pre><span>### Load the Needed Libraries</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>ggQC<span>)</span>
 
<span>### Make up some demo data (load your file here instead)</span>
<span>set.<span>seed</span></span><span>(</span><span>5555</span><span>)</span>
Process_Data <span>&lt;-</span> 
  <span>data.<span>frame</span></span><span>(</span>
   Process<span>=</span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;A&quot;</span><span>)</span>, each <span>=</span> <span>30</span><span>)</span>, <span>#Process A </span>
   Run_Number<span>=</span><span>c</span><span>(</span><span>1</span><span>:</span><span>30</span><span>)</span>,             <span>#Run Order </span>
   Value <span>=</span> <span>c</span><span>(</span><span>rnorm</span><span>(</span>n <span>=</span> <span>30</span>, <span>mean</span> <span>=</span> <span>30.5</span>, <span>sd</span> <span>=</span> <span>1</span><span>)</span><span>)</span> <span>#Process A Random Data</span>
   <span>)</span>
 
<span>### Make the plot</span>
XmR_Plot <span>&lt;-</span> 
  ggplot<span>(</span>Process_Data, aes<span>(</span>x <span>=</span> Run_Number, y <span>=</span> Value<span>)</span><span>)</span> <span>+</span> <span>#init ggplot</span>
  geom_point<span>(</span><span>)</span> <span>+</span> geom_line<span>(</span><span>)</span> <span>+</span> <span># add the points and lines</span>
  stat_QC<span>(</span>method <span>=</span> <span>&quot;XmR&quot;</span>,      <span># specify QC charting method</span>
          auto.<span>label</span> <span>=</span> <span>T</span>,      <span># Use Autolabels</span>
          label.<span>digits</span> <span>=</span> <span>2</span>,    <span># Use two digit in the label</span>
          show.1n2.<span>sigma</span> <span>=</span> <span>T</span>   <span># Show 1 and two sigma lines</span>
          <span>)</span> <span>+</span>  
  scale_x_continuous<span>(</span>expand <span>=</span>  expand_scale<span>(</span>mult <span>=</span> .15<span>)</span><span>)</span>  <span># Pad the x-axis</span>
 
<span>### Draw the plot - Done</span>
XmR_Plot</pre></td></tr></table></div>
<p><img alt="XmR control chart with ggQC" src="https://i2.wp.com/cdn.r-bar.net/PID627/basic_control_chart.png?w=456" data-recalc-dims="1"></p>
<p>By building upon the ggplot framework, you get a high level of control over the plot details such as points and lines etc. In addition, if you want to put XmR and mR data on the same plot, you can. Just make multiple calls to the stat_QC() command, as shown below. </p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td><pre><span>### Two stat_QC calls</span>
XmR_Plot <span>&lt;-</span> 
  ggplot<span>(</span>Process_Data, aes<span>(</span>x <span>=</span> Run_Number, y <span>=</span> Value<span>)</span><span>)</span> <span>+</span> <span>#init ggplot</span>
  geom_point<span>(</span><span>)</span> <span>+</span> geom_line<span>(</span><span>)</span> <span>+</span> <span>#add the points and lines</span>
  stat_QC<span>(</span>method <span>=</span> <span>&quot;XmR&quot;</span>, <span>#specify QC charting method</span>
          auto.<span>label</span> <span>=</span> <span>T</span>, <span># Use Autolabels</span>
          label.<span>digits</span> <span>=</span> <span>2</span>, <span>#Use two digit in the label</span>
          show.1n2.<span>sigma</span> <span>=</span> <span>T</span>  <span>#Show 1 and two sigma lines</span>
          <span>)</span> <span>+</span>  
  stat_QC<span>(</span>method<span>=</span><span>&quot;mR&quot;</span><span>)</span> <span>+</span>
  scale_x_continuous<span>(</span>expand <span>=</span>  expand_scale<span>(</span>mult <span>=</span> .15<span>)</span><span>)</span>  <span># Pad the x-axis</span>
 
<span>### Draw the plot - Done</span>
XmR_Plot</pre></td></tr></table></div>
<p><img alt="multi stat_QC control chart" src="https://i2.wp.com/cdn.r-bar.net/PID627/multi_stat_QC_Call.PNG?w=456" data-recalc-dims="1"></p>
<p>For more control chart examples, checkout the docs, HOWTOs, and Vignettes at <a rel="nofollow" href="http://rcontrolcharts.com/" target="_blank">rcontrolcharts.com</a>. </p>
<h3>Violation Analysis
<p>To check for out of control data in your process, use the stat_qc_violations() command. When you run this command, your data is checked against the following 4 Shewart violation rules:</p>
<ul>
<li> <b>Same Side</b>: 8 or more consecutive, same-side points </li>
<li> <b>1 Sigma</b>: 4 or more consecutive, same-side points exceeding 1 sigma </li>
<li> <b>2 Sigma</b>: 2 or more consecutive, same-side points exceeding 2 sigma </li>
<li> <b>3 Sigma</b>: any points exceeding 3 sigma </li>
</ul>
<p>This next bit of code demonstrates a violation analysis with the stat_qc_violation() command using process data from the previous section. </p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
</pre></td><td><pre><span>#Uses the same data as previous example. </span>
 
QC_Violations <span>&lt;-</span> 
  ggplot<span>(</span>Process_Data, aes<span>(</span>x <span>=</span> Run_Number, y <span>=</span> Value<span>)</span><span>)</span> <span>+</span> <span>#init ggplot</span>
  stat_qc_violations<span>(</span>method <span>=</span> <span>&quot;XmR&quot;</span> 
                     <span>#show.facets = 4 #if you just want facet 4</span>
                     <span>)</span>
QC_Violations</pre></td></tr></table></div>
<p>After executing the code, you should see a plot with 4 facets – one for each Shewart rule. If you only want to see the 4th facet, set show.facets = 4. Other settings such as show.facets = c(2, 4) will show 1 and 3 sigma violations, only. </p>
<p><img alt="Control Chart Violation Analysis with ggQC" src="https://i1.wp.com/cdn.r-bar.net/PID627/violation_analysis.png?w=456" data-recalc-dims="1"></p>
<p>For our test data, none of the standard 4 Shewart violations were observed. Awesome! Next, we’ll look at doing a capability analysis with ggQC. </p>
<h3>Capability Analysis</h3>
<p>In the previous sections, you learned how to make a control chart with ggQC and check for violations. Here you'll learn how to do a basic capability analysis (Cp, Cpk, Pp, Ppk etc.). For this, we assume the customer has a lower specification limit (LSL) and upper specification limit (USL) of 25 and 37, respectively. With these specifications and the stat_QC_Capability() command, you can do a graphical capability analysis in just a few simple lines of code:</p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td><pre><span># Uses the same data as the first example</span>
CapabilityAnaylsis <span>&lt;-</span> 
 ggplot<span>(</span>Process_Data, aes<span>(</span>x <span>=</span> Value<span>)</span><span>)</span> <span>+</span> <span>#init ggplot</span>
  geom_histogram<span>(</span>binwidth <span>=</span> .75, color<span>=</span><span>&quot;purple&quot;</span><span>)</span> <span>+</span> <span>#make the histogram</span>
  stat_QC_Capability<span>(</span>
    LSL<span>=</span><span>25</span>, USL<span>=</span><span>37</span>,                    <span>#Specify LSL and USL</span>
    show.<span>cap</span>.<span>summary</span> <span>=</span> <span>c</span><span>(</span><span>&quot;Cp&quot;</span>, <span>&quot;Cpk&quot;</span><span>)</span>, <span>#selected summary</span>
    digits <span>=</span> <span>2</span>,                        <span>#report two digits</span>
    method<span>=</span><span>&quot;XmR&quot;</span><span>)</span> <span>+</span>                    <span>#Use the XmR method</span>
scale_x_continuous<span>(</span>expand <span>=</span>  expand_scale<span>(</span>mult <span>=</span> <span>c</span><span>(</span><span>0.15</span>,.65<span>)</span><span>)</span><span>)</span> <span>#pad the X-axis </span>
 
<span>#plot the graph</span>
CapabilityAnaylsis</pre></td></tr></table></div>
<p>To adjust the capability metrics displayed on the plot, provide the show.cap.summary argument with a vector of desired metrics. Metrics available include:</p>
<ul>
<li><strong>TOL:</strong> Tolerance in Sigma Units (USL-LSL)/sigma</li>
<li><strong>DNS:</strong> Distance to Nearest Specification Limit in Sigma Units</li>
<li><strong>Cp:</strong> Cp (Within sample elbow-room metric)</li>
<li><strong>Cpk:</strong> Cpk (Within sample centering metric)</li>
<li><strong>Pp:</strong> Pp (Between sample elbow-room metric)</li>
<li><strong>Ppk:</strong> Ppk (Between sample centering metric)</li>
<li><strong>LCL:</strong> Lower Control Limit</li>
<li><strong>X:</strong> Process Center</li>
<li><strong>UCL:</strong> Upper Control Limit</li>
<li><strong>Sig:</strong> Sigma from control charts</li>
</ul>
<p>The order given in the vector is the order displayed on the chart. In this case, only Cp and Cpk were selected, as shown below. </p>
<p><img alt="Process Capability Analysis with ggQC" src="https://i1.wp.com/cdn.r-bar.net/PID627/capability_analysis.png?w=456" data-recalc-dims="1"></p>
<p>Cool! Looks like the process is in good shape. To see more examples of capability analysis, checkout the ggQC documentation and examples on <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/" target="_blank">stat_QC_Capability</a>. stat_QC_Capability is also compatible with ggplot faceting. Note that XbarR capability charts are specified slightly different than XmR.</p>
<h3>Pareto Analysis</h3>
<p>Alright, so your processes are in control. However, you know your process has bottlenecks. Where should you start? One way to help plan your attack is with a Pareto analysis. Suppose you have the following data showing how long several typical process steps take. </p>
<p><img alt="Pareto Data" src="https://i1.wp.com/cdn.r-bar.net/PID627/ParetoData_Table.png?w=456" data-recalc-dims="1"></p>
<p>To generate a Pareto chart, load the data, initialize ggplot, and let the stat_pareto() command do the rest. </p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td><pre> 
<span>#load your data</span>
Data4Pareto <span>&lt;-</span> <span>data.<span>frame</span></span><span>(</span>
  KPI <span>=</span> <span>c</span><span>(</span><span>&quot;Customer Service Time&quot;</span>, <span>&quot;Order Fulfillment&quot;</span>, <span>&quot;Order Processing Time&quot;</span>,
        <span>&quot;Order Production Time&quot;</span>, <span>&quot;Order Quality Control Time&quot;</span>, <span>&quot;Rework Time&quot;</span>,
        <span>&quot;Shipping&quot;</span><span>)</span>,
  Time <span>=</span> <span>c</span><span>(</span><span>1.50</span>, <span>38.50</span>, <span>3.75</span>, <span>23.08</span>, <span>1.92</span>, <span>3.58</span>, <span>73.17</span><span>)</span>
<span>)</span> 
 
<span>#make the plot</span>
ggplot<span>(</span>Data4Pareto, aes<span>(</span>x<span>=</span>KPI, y<span>=</span>Time<span>)</span><span>)</span> <span>+</span>
 stat_pareto<span>(</span>point.<span>color</span> <span>=</span> <span>&quot;red&quot;</span>,
             point.<span>size</span> <span>=</span> <span>3</span>,
             line.<span>color</span> <span>=</span> <span>&quot;black&quot;</span>,
             bars.<span>fill</span> <span>=</span> <span>c</span><span>(</span><span>&quot;blue&quot;</span>, <span>&quot;orange&quot;</span><span>)</span>
             <span>)</span> <span>+</span>
  theme<span>(</span>axis.<span>text</span>.<span>x</span> <span>=</span> element_text<span>(</span>angle <span>=</span> <span>90</span>, hjust <span>=</span> <span>1</span>, vjust<span>=</span><span>0.5</span><span>)</span><span>)</span>
 
<span>#done</span></pre></td></tr></table></div>
<p><img alt="Pareto Analysis with ggQC" src="https://i0.wp.com/cdn.r-bar.net/PID627/Pareto_Analysis.png?w=456" data-recalc-dims="1"></p>
<p>Looks like our next improvement project will focus on either shipping or order fulfillment. Good Luck! </p>
<h3>Summary</h3>
<p>Building control charts with ggQC is quick and easy, especially if you're already familiar with ggplot. Like other ggplot graphs, ggQC control charts support faceting and are built up layer-by-layer. If you need to make a complicated chart, go ahead. You can add as many stat_QC calls as you like (see <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/" target="_blank">XbarR_Vignette</a>). In addition to control charting, ggQC allows you to run Pareto, capability, and Shewart violation analysis. To learn more, please visit <a rel="nofollow" href="http://rcontrolcharts.com/" target="_blank">rcontrolcharts.com</a> </p>
<hr>
<h3>Other Useful Links</h3>
<ul>
<li><a rel="nofollow" href="https://r-bar.net/estimating-control-chart-constants-with-r/" target="_blank">An Explanation of Control Chart Constants</a></li>
</ul>
<p>The post <a rel="nofollow" href="http://r-bar.net/ggqc-ggplot-quality-control-charts/" target="_blank">ggQC | ggplot Quality Control Charts – New Release</a> appeared first on <a rel="nofollow" href="http://r-bar.net/" target="_blank">R-BAR</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-bar.net/ggqc-ggplot-quality-control-charts/"> R – R-BAR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/2Ti48e3Lgm0" height="1"></a></div></h3>