Change the Title in a process.capability plot

478 Views Asked by At

I am using the qcc package. I am running a simple process.capability plot inside of a loop for multiple materials. As it loops through the columns, the title of the plot is always the df.1[i], rather than the column name.

process.capability(qcc(df.1[i], type="xbar.one"), nsigmas = 3, spec.limits = c(NA, 20), add.stats = T)

Or conversely, is it possible to use the actual column name while still in the loop?

process.capability(qcc(df.1$Aluminum, type="xbar.one"), nsigmas = 3, spec.limits = c(NA, 20), add.stats = T)

Full loop below:

for (i in 5:length(colnames(df.1))){
    df.2 <- df.1 %>%
    rename(test_metal = colnames(df.1[i]))
  test_group = with(df.2, qcc.groups(test_metal, batch_num))
  png("test_plot.png")
  q1 <- qcc(test_group, type = "xbar.one", nsigmas = 3, chart.all = F, ylim = c(0, max(df.1$Aluminum)), title = paste(colnames(df.1)[i]))
  dev.off()
}
2

There are 2 best solutions below

0
On

You can use a combination of eval and substitute to have the call for process.capability in the correct form. I used an example from the qcc package:

library(qcc)

df.1 <- pistonrings
df.1 = cbind(pistonrings,
replicate(3,pistonrings$diameter+rpois(nrow(pistonrings),3))
)
colnames(df.1)[4:6] = c("d1","d2","d3")
pdf("test.pdf")
for (i in colnames(df.1)[4:6]){
  eval(
  substitute(
  process.capability(qcc(X, type="xbar.one"), 
  nsigmas = 3, spec.limits = c(NA, 20), add.stats = T),
  list(X=as.name(i))
  ),df.1)
}
dev.off()

Brief explanation, first you use substitute to replace "X" with the colname you want to plot. Then this remains an expression and you have to use eval on it, together with the data.frame df.1. (something like "with" you used before)

enter image description here

0
On

Since I posted this, Luca has added a new capability to edit the title, which obviates the need for this. So, for this I simply added a title call in the function.

title = paste(colnames(df.1)[i]))