Created
June 18, 2024 21:23
-
-
Save py/68f67fb14f2c209e7efd18c43249147d to your computer and use it in GitHub Desktop.
QCC's xbar chart recreated in ggplot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load libraries ------- | |
library(ggplot2) | |
library(dplyr) | |
# Function to create X-bar chart | |
create_xbar_chart <- function( | |
data, | |
x_var, | |
y_var, | |
title = "" | |
) { | |
# Constants | |
annot_hjust <- 0.5 | |
cl_weight <- 0.9 | |
xbar_color <- "blue" | |
cl_color <- "red" | |
# Capture variable names | |
x_var <- enquo(x_var) | |
y_var <- enquo(y_var) | |
# Calculate control limits | |
values <- data %>% pull(!!y_var) | |
x_bar <- mean(values) | |
sigma <- sd(values) | |
n <- length(values) | |
UCL <- x_bar + 3 * sigma / sqrt(n) | |
LCL <- x_bar - 3 * sigma / sqrt(n) | |
# Create a new column for color coding | |
data <- data %>% mutate(status = ifelse(!!y_var > UCL | !!y_var < LCL, "Out of Control", "In Control")) | |
# Get the minimum x value for annotations | |
min_x <- min(data %>% pull(!!x_var)) | |
max_x <- max(data %>% pull(!!x_var), na.rm = TRUE) | |
# Create X-bar chart | |
ggplot(data) + | |
# geom_rect(aes(xmin = min_x, xmax = max_x, ymin = LCL, ymax = UCL), fill = "lightgray", alpha = 0.5) + | |
geom_line(aes(x = !!x_var, y = !!y_var), color = "black") + | |
geom_hline(yintercept = x_bar, color = xbar_color) + | |
geom_hline(yintercept = UCL, linetype = "dashed", color = cl_color, linewidth = cl_weight) + | |
geom_hline(yintercept = LCL, linetype = "dashed", color = cl_color, linewidth = cl_weight) + | |
geom_point(size = 5, color = "white", aes(x = !!x_var, y = !!y_var)) + | |
geom_point(size = 3, aes(x = !!x_var, y = !!y_var, color = status)) + | |
annotate("text", x = max_x, y = UCL, label = paste("UCL =", round(UCL, 2)), hjust = annot_hjust, vjust = -0.5, color = "red") + | |
annotate("text", x = max_x, y = LCL, label = paste("LCL =", round(LCL, 2)), hjust = annot_hjust, vjust = 1.5, color = "red") + | |
annotate("text", x = max_x, y = x_bar, label = paste("CL =", round(x_bar, 2)), hjust = annot_hjust, vjust = -0.5, color = "blue") + | |
labs( | |
title = paste0("X-Bar Chart: ", title), | |
color = "Status" | |
) + | |
scale_color_manual(values = c("In Control" = "black", "Out of Control" = "red")) + | |
# theme_ipsum_rc() + | |
theme_minimal() + | |
theme(legend.position = "top") | |
} | |
# Testing----------------- | |
## Create dummy dataframe ------- | |
set.seed(123) | |
example_data <- data.frame( | |
date = as.Date('2023-01-01') + 0:24, | |
value = rnorm(25, mean = 10, sd = 1) | |
) | |
## Create the chart -------- | |
create_xbar_chart( | |
data = example_data, | |
x_var = date, | |
y_var = value, | |
title = "Test Data" | |
) + | |
labs( | |
subtitle = "It is a ggplot2 object, so continue customizing the plot as you normally would!", | |
caption = "Peter Yates www.linkedin.com/in/peteryates1 2024.", | |
x = "Date", | |
y = "Average Daily Measurement" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment