How do I facet a ggplot2 based graph from qicharts2 package?

stackinator

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?

Tung

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I facet by geom / layer in ggplot2?

How do I plot a mle2 fit from frair package in ggplot2?

How to calculate % of a label in column and use this in ggplot2 facet graph

How to use qicharts2 and ggplots2 together?

How can I annotate facets that do not have data using ggplot2 facet_grid?

How do I make donut charts with facet_grid in ggplot2?

R: GGPLOT2: How do I add labels in a line graph in ggplot2 using stat_summary

How to Delete Facets from ggplot2 facet_wrap?

How do I correct an error in not being able to generate my ggplot2 graph?

How do you graph two variables from a .csv file as two lines in ggplot2?

How can I make an alluvial graph using ggplot2 to show the results of cross tabulation from spss?

How can I make a stacked bar graph from a set of wide data using ggplot2?

How can I sort a bar graph from highest to lowest in R? (ggplot2)

How can I use ggplot2's facet_grid to plot every combination of discrete values from one column?

facet_grid in ggplot2 returning a graph for (all), how to remove

How do I change the color of my bar plot using ggplot2 package of R?

How do I construct a crosswalk table with multiple crosswalks with the ggplot2 package?

How to order axis by facet in ggplot2?

How do I remove labels based on certain conditions in ggplot2?

ggplot2 missing facet wrap label on graph

How do I add a line graph from another dataset to an existing line graph where facet grid has been used?

ggplot2 - How can I have two colour schemes when using facet_wrap?

r - How can I get a lengthy label to fit using facet_grid in ggplot2?

how can I use math expressions in ggplot2 facet labels

How do I create x-axis labels from two categorical variables in ggplot2?

ggplot2: How do I rearrange the bars from highest to lowest?

How to draw a ggplot2 with facet_wrap, showing percentages from each group, not overall percentages?

How do I plot a graph by based on 3 groups in ggplot?

How do I reproduce a plot in ggplot based on nonlinear regressions from the R package Growthrates?