luca-scr/qcc

Exporting control chart files through the png function

mattlitz opened this issue · 2 comments

With qcc 3.0, I am attempting to generate multiple control and process capability charts queried from a database table that contains data on hundreds of features I'm interested in. For each feature, I subset the feature data into a data frame and insert into a list of data frames. I then loop through the list of data frames, generate the charts, and export the image file to a folder using the .png() function. This was possible in qcc 2.7, however I am getting blank white images with qcc 3.0. The png() function does work for a single data frame; the problem arises as I loop through a list of data frames. Here is some sample code and I appreciate your help!

#create list of dataframes
df_list <- list()

for(i in 1:5){
  obs <- 1:20
  data <- rnorm(20, mean=5, sd=4)
  df <- data.frame(obs,data)
  df_list[[i]] <- df
}


#loop through list of dataframes and export control charts as a .png
for(n in 1:length(df_list)){
  
    png(filename = paste0("control_chart_",n,".png"), width=640, height=480)
    
    q1 <- qcc(df_list[[n]]$data,
    type = "xbar.one",
    limits = c(4,6))
    plot(q1)
    pc <- processCapability(q1, spec.limits = c(3,7))
    plot(pc)
    
    dev.off()
}

And one more issue I noticed is that RStudio cuts off the last line of the control chart data with StdDev, UCL, and Number Violating Runs. This isn't a problem in RGui - could this be an issue with RStudio?

First of all, TTBOMK png() won't save multiple plots on the same file.
Second, png() + dev.off() is not the recommended way to save ggplot2 graphs. Instead use ggsave().

Here is the code for your example:

library(qcc)
df_list <- list()
for(i in 1:5)
{
  obs <- 1:20
  data <- rnorm(20, mean=5, sd=4)
  df <- data.frame(obs,data)
  df_list[[i]] <- df
}

for(n in 1:length(df_list))
{
  q1 <- qcc(df_list[[n]]$data,
            type = "xbar.one",
            limits = c(4,6))
  ggsave(plot = plot(q1), 
         filename = paste0("control_chart_", n, "a.png"), 
         device = "png", width = 6.4, height = 4.8)
  
  pc <- processCapability(q1, spec.limits = c(3,7))
  ggsave(plot = plot(pc), 
         filename = paste0("control_chart_", n, "b.png"), 
         device = "png", width = 6.4, height = 4.8)
}

This is exactly what I needed! Thank you so much for the code and thorough response Dr. Scrucca!