top of page

Building Control Charts in R for Process Monitoring

laptop screen displaying line of code

What is a Control Chart?

A control chart is a graphical tool used in statistical process control (SPC) to monitor the performance and stability of a process over time. It helps distinguish between normal process variation (common causes) and unusual variation (special causes), enabling timely interventions and continuous improvement.

A control chart is a time-series plot that displays process data points in chronological order, along with three key reference lines:

  • Center Line (CL): Represents the process average or expected value.

  • Upper Control Limit (UCL): The threshold above which variation is considered statistically unlikely (typically set at +3 standard deviations from the mean).

  • Lower Control Limit (LCL): The threshold below which variation is considered statistically unlikely (typically set at −3 standard deviations from the mean).

Example of a Control Chart

example of a control chart

Step-by-Step Guide

Load the required libraries.

# load libraries
library(tidyverse)
library(qicharts2)

Load data.

# load data
superstore<-read.csv("https://raw.githubusercontent.com/bernardkilonzo-rigor/dataviz/main/data/Sample%20-%20Superstore.csv")

Extracting week date part from Order Date.

# extracting week date part from order date
superstore<-superstore%>%mutate(Order.Date = dmy(Order.Date))%>%
mutate(week = week(Order.Date))

Computing sales by week, control line (cl), upper control limit (ucl), and lower control limit (lcl).

# computing sales by week, cl, ucl and lcl
super_cal<-superstore%>%group_by(week)%>%
summarise(sales =sum(Sales))%>%
mutate(cl = mean(sales),
sd = sd(sales),
ucl = mean(sales)+2*sd,
lcl = mean(sales)-2*sd)

Creating a basic control chart.

# creating control chart
q<-qic(sales,data = super_cal, chart="xbar")

Executing the above code generates the view below.

a basic control chart build in R

Formatting the control chart.

qc<-q+
geom_hline(yintercept = super_cal$ucl, linetype = "dashed", color = "red") +
geom_hline(yintercept = super_cal$lcl, linetype = "dashed", color = "blue")+
geom_hline(yintercept = super_cal$cl,  linetype = "solid",  color = "black") +
labs(title = "Control Chart with Manual Limits",x = "weeks", y = "Sales", caption = "Viz by: Bernard Kilonzo")+
theme(axis.title = element_text(family = "serif", face = "bold", size = 10, color = "gray30"),
axis.text = element_text(family = "serif", size = 9, color = "gray30"),
plot.title = element_text(family = "serif", face = "bold", size = 13, color = "gray20"),
plot.caption = element_text(family = "serif", face = "italic", size = 9, color = "gray40"))

Executing the above code generates the view below.

final control chart build in R

Conclusion

Mastering control charts with qicharts2 equips you with a powerful toolkit for tracking process stability and driving data-informed decisions. Its streamlined syntax and thoughtful defaults make it ideal for healthcare, manufacturing, and service industries alike. By integrating statistical rules and visual cues, you can detect meaningful variation and respond proactively. Whether you're building dashboards or conducting quality improvement audits, qicharts2 helps transform routine data into strategic insight - one chart at a time.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!!

Tags:

 
 
Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

WhatsApp: +254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page