Created
February 14, 2014 13:01
-
-
Save tomhopper/9000586 to your computer and use it in GitHub Desktop.
Demonstration of integration of plotting individuals chart using qcc and ggplot2, with annotation outside of the plot.
This file contains hidden or 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
library(qcc) | |
library(ggplot2) | |
library(grid) | |
#' The data, from sample data provided by Donald Wheeler | |
my.xmr.raw <- c(5045,4350,4350,3975,4290,4430,4485,4285,3980,3925,3645,3760,3300,3685,3463,5200) | |
#' Create the individuals qcc object | |
my.xmr.x <- qcc(my.xmr.raw, type = "xbar.one", plot = FALSE) | |
#' create a data frame from the individuals qcc object | |
my.xmr.df <- data.frame(index = 1:length(my.xmr.raw), x = my.xmr.raw) | |
#' Make pretty axis limits so we can use them later for positioning text | |
my.ylim <- range(c(my.xmr.x$statistics, my.xmr.x$limits[2], my.xmr.x$limits[1])) | |
my.xlim <- range(1:length(my.xmr.x$statistics)) | |
#' Create the basic graph | |
#' Set font sizes to be consistent between ggplot elements and grid elements | |
my.p <- ggplot(data = my.xmr.df) + | |
theme(text = element_text(size = 24), | |
plot.margin = unit(c(2,4,1,1), "char")) + | |
scale_x_continuous(expand = c(0, 0.5), limits = my.xlim) + | |
geom_point(aes(x = index,y = x),shape = 20) + | |
geom_line(aes(x = index,y = x)) + | |
geom_point(aes(x = index[x > 5100],y = x[x > 5100]),shape = 19,colour = '#ff0033') | |
#' | |
my.p <- my.p + | |
ylim(my.ylim) | |
# draw the UCL and LCL lines | |
my.p <- my.p + | |
stat_hline(yintercept = my.xmr.x$limits[2],linetype = 2) + | |
stat_hline(yintercept = my.xmr.x$limits[1],linetype = 2) | |
#' Label the limits | |
my.p <- my.p + | |
annotation_custom(grob = textGrob("LCL", hjust = 0, gp = gpar(fontsize = 24)), | |
xmin = length(my.xmr.x$statistics) + 0.6, | |
xmax = length(my.xmr.x$statistics) + 0.6, | |
ymin = my.xmr.x$limits[1], | |
ymax = my.xmr.x$limits[1]) | |
my.p <- my.p + | |
annotation_custom(grob = textGrob("UCL", hjust = 0, vjust = 0.5, gp = gpar(fontsize = 24)), | |
xmin = length(my.xmr.x$statistics) + 0.6, | |
xmax = length(my.xmr.x$statistics) + 0.6, | |
ymin = my.xmr.x$limits[2], | |
ymax = my.xmr.x$limits[2]) | |
#' Draw the center line | |
my.p <- my.p + | |
stat_hline(yintercept = my.xmr.x$center, linetype = 1) | |
#' and label it | |
my.p <- my.p + | |
annotation_custom(grob = textGrob("CL", hjust = 0, vjust = 0.5, gp = gpar(fontsize = 24)), | |
xmin = length(my.xmr.x$statistics) + 0.6, | |
xmax = length(my.xmr.x$statistics) + 0.6, | |
ymin = my.xmr.x$center, | |
ymax = my.xmr.x$center) | |
#' And now the magic: disable clipping so the labels can be plotted outside the plot region | |
my.gt <- ggplot_gtable(ggplot_build(my.p)) | |
my.gt$layout$clip[my.gt$layout$name == "panel"] <- "off" | |
#' Finally, draw the graph | |
grid.draw(my.gt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
love the idea of using the qcc module stats this way. Nicely done.