How to plot x-bar and r charts in r

In this article we’ll be looking at how to plot an Xbar and R chart using R programming. But before we do that, let us first understand the concept behind it. If you are familiar with the concept, you can just skip ahead and look at an example below.

Definition

An xbar chart is a graphical representation of the average value of a data set over a period of time. The x-axis of the chart represents time, and the y-axis represents the average value.

An R chart is a type of statistical chart used to monitor the quality of data over time. It is used to track the variation in the data and to identify any outlying data points.

Variables and Attributes

It is important to understand the difference between the two.

Quality characteristics fall into two broad classes

  • Variables
  • Attributes

Characteristics that are measurable and are expressed on a numerical scale are called variables like length, width, height, diameter, volume etc.

A quality characteristic that cannot be measured on a numerical scale is referred to as an attribute e.g performance, realibilty, appearance, availabilty, safety, usability etc

Control Charts for Variables

Control charts for variables are powerful tools that can be used when measurements from a process are available. Variable charts explain process data in terms of both spread (piece-to-piece varianility) and location (process average)

Control charts for variables are prepared from quantitative data e.g diameter, weight, height etc.

Note that control charts for variables are preapred and analyzed in pairs i.e locaion and spread.

The most commonly used pair of control charts; x-bar chart and R chart.

The X-bar is the average of the values in small sub-groups, and R is the range of values within each sub-group, and the R chart is the range of values within each sub-group.

Control limits for R charts

UCL_R=D_4\overline{R}

and

LCL_R=D_3\overline{R}

where

D_3=1-\frac{3d_3}{d_2} D_4=1+\frac{3d_3}{d_2}

where

d_2=\int_{-\infty}^{\infty}\left[1-\left(1-\alpha\right)^n\right]dx_l

and

d_3=2\int_{-\infty}^{\infty}\int_{-\infty}^{x_l}\left(1-\alpha_l^n-\left(1-\alpha_n\right)^n+\left(\alpha_l-\alpha_n\right)^n\right)

and

\alpha_l=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x_l}e^{-\left(\frac{x^2}{2}\right)}dx

and

\alpha_n=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x_n}e^{-\left(\frac{x^2}{2}\right)}dx

n is the sample size

\overline{R} is the mean of the range of values in each subgroup.

Control limits for X-bar Chart

UCL=\overline{\overline{R}}+A_2\overline{R}

and

LCL=\overline{\overline{X}}-A_2\overline{R}

where

A_2=\frac{3}{d_2\left(\sqrt{n}\right)}

[lat1ex] A_2,\ D_3\ and\ D_4 [/latex] are factors that depend on subgroup size.

A_2 is the factor for controlling for averages and D_2\ and\ D_4 are the factors for controlling for ranges.

Table of Constants

nA2D3D4
21.88003.267
31.02302.575
40.72902.282
50.57702.114
60.48302.004
70.4190.0761.924
80.3730.1361.864
90.3370.1841.816
100.3080.2231.777

Example

Subgroup1234mean(x)R
11410121312.254
2144151012.755
310111011.510.631.5
412151714.514.635
51381011.510.635
6810.59119.632
71112.59810.134.5
81615.5131514.882.5
91613102014.7510
101011151512.755
11181514.51615.883.5
12151313.51213.383
131419131214.507
141618111114.007
151616151515.501
161615.5101413.886
1716.518182018.133.5
1815181816.516.883
191316171515.254
20161817.51917.633
211817131415.505
221610.5101613.136
231516151314.753
2411.514151012.635
251612151614.754

Limits for R chart

UCL_R=D_4\cdot\overline{R}

= 2.28 * 4.34

= 9.9

LCL_R=D_3\cdot\overline{R}

= 0*4.34

= 0

Limits for X-bar Chart

UCL=\overline{\overline{x}}+A_2\cdot\overline{R}

= 13.96+0.73*4.34

= 17.1

LCL=\overline{x}-A_2\cdot\overline{R}

= 13.96-0.73*4.34

= 10.8

Plotting the above data for X-bar and R chart in R

Note that if you were to do data entry for the data presented above. You’ll need to input it in this format

how to plot an x-bar and r chart in r
library(qcc)

charts.data <- read.csv("SQC.csv")
charts.data
attach(charts.data)

#To view the last six datasets
tail(charts.data)
obs subgroup
95   15       24
96   10       24
97   16       25
98   12       25
99   15       25
100  16       25


#creating a qcc object from the data
measurements <- qcc.groups(obs,subgroup)
head(measurements)
[,1] [,2] [,3] [,4]
1   14 10.0   12 13.0
2   14  4.0   15 10.0
3   10 11.0   10 11.5
4   12 15.0   17 14.5
5   13  8.0   10 11.5
6    8 10.5    9 11.0

tail(measurements)
[,1] [,2] [,3] [,4]
20 16.0 18.0 17.5   19
21 18.0 17.0 13.0   14
22 16.0 10.5 10.0   16
23 15.0 16.0 15.0   13
24 11.5 14.0 15.0   10
25 16.0 12.0 15.0   16

#summary of the data
summary(measurements)
V1              V2              V3              V4       
Min.   : 8.00   Min.   : 4.00   Min.   : 9.00   Min.   : 8.00  
1st Qu.:13.00   1st Qu.:11.00   1st Qu.:10.00   1st Qu.:11.50  
Median :15.00   Median :15.00   Median :13.50   Median :14.00  
Mean   :14.24   Mean   :13.86   Mean   :13.42   Mean   :13.96  
3rd Qu.:16.00   3rd Qu.:16.00   3rd Qu.:15.00   3rd Qu.:16.00  
Max.   :18.00   Max.   :19.00   Max.   :18.00   Max.   :20.00  
#R charts
qcc(measurements,type="R")
how to plot X-bar and R chart in R
#X-bar chart
qcc(measurements,type="xbar")

By Benard Mbithi

A statistics graduate with a knack for crafting data-powered business solutions. I assist businesses in overcoming challenges and achieving their goals through strategic data analysis and problem-solving expertise.