qicharts2 is a statistical process control package that plots control charts. Let us plot an example chart:
library(qicharts2)
library(ggplot2)
(plot1 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
exclude = c(45, 70),
title = 'Age of the last 100 patients (I chart)',
ylab = 'Years',
xlab = 'Patient #')
)
p1 <- plot1$data
I can customize this chart pulling the data from it plot1$data
and doing typical ggplot2 customization. See below.
(plot2 <- ggplot(plot1$data, aes(x,y)) +
geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "green", alpha = 0.4) +
geom_line(colour = "blue", size = .75) +
geom_line(aes(x, cl)) +
geom_point(colour = "black" , fill = "black", size = 1.5) +
ggtitle(label = "example i chart") +
labs(x = NULL,
y = NULL)+
theme_minimal()
)
Let us go ahead and facet plot 1 by gender, and we'll call it plot 3.
(plot3 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
exclude = c(45, 70),
title = 'Age of the last 100 patients (I chart)',
ylab = 'Years',
xlab = 'Patient #',
facet = ~ gender)
)
p3 <- plot3$data
I want to create a plot 4
that uses the same options as plot 2
but is faceted like plot 3
. What do I need to add to my second chunk of code to make it faceted by gender?
The facet
you're looking for is stored in plot3$data$facet1
column
library(qicharts2)
library(ggplot2)
plot3 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
exclude = c(45, 70),
title = 'Age of the last 100 patients (I chart)',
ylab = 'Years',
xlab = 'Patient #',
facet = ~ gender)
p3 <- plot3$data
plot4 <- ggplot(p3, aes(x,y)) +
geom_ribbon(ymin = lcl, ymax = ucl, fill = "green", alpha = 0.4) +
geom_line(colour = "blue", size = .75) +
geom_line(aes(x, cl)) +
geom_point(colour = "black" , fill = "black", size = 1.5) +
ggtitle(label = "example i chart") +
labs(x = NULL,
y = NULL)+
theme_minimal() +
facet_grid(~ facet1)
plot4
Created on 2018-03-23 by the reprex package (v0.2.0).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments