qicharts2 Control Limits in R

813 Views Asked by At

I am using the qicharts2() package to construct a p-chart in R. It is necessary to have a variable UCL / LCL, but the way the qic() natively constructs this is not what I'm looking for. See the below two images:

What qic() produces: enter image description here

What I need it to produce: enter image description here

I'm unsure how to change this and couldn't find much to help control the UCL/LCL in the help vignette. Any help on how to control these aesthetics or the calculation going into them is appreciated (I am not a statistician). Sample:

df <- data.frame(Date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 25), 
                 Values = sample(seq(from = 0, to = 1, by = .1), size = 25, replace = TRUE), 
                 Totals = sample(seq(from = 0, to = 50, by = 1), size = 25, replace = TRUE))

qic(data = df, y = Values, x = Date, n = Totals, chart = 'p', point.size = 2)
1

There are 1 best solutions below

0
On

Thanks to the comments from @markus, the key was to save the qic() gg object to a variable and access the layers. Using the code below demonstrates how this works:


df <- data.frame(Date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 25), 
                 Values = sample(seq(from = 0, to = 1, by = .1), size = 25, replace = TRUE), 
                 Totals = sample(seq(from = 0, to = 50, by = 1), size = 25, replace = TRUE))

p <- qic(data = df, y = Values, x = Date, n = Totals, chart = 'p', point.size = 2, show.labels = TRUE, decimals = 0) +
  geom_line(color = "steelblue") + theme_bw() +
  ylim(c(0,1)) +
  ggtitle("Sample qic() Plot") +
  xlab("") +
  ylab("") +
  theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"), 
        axis.title.y = element_text(face = "bold", size = 12)) +
  theme(axis.text.x = element_text(angle = 65, hjust = 1, size = 12, face = "bold"), 
        axis.text.y = element_text(size = 12, face = "bold")) +
  theme(legend.position = "none")

p$layers[[1]] <- NULL; 
p$layers <- c(p$layers, geom_step(data = p$data, aes(y = ucl), linetype = "dotted", col = "grey50", size = 1), geom_step(data = p$data, aes(y = lcl), linetype = "dotted", col = "grey50", size = 1)); 
p

Output:

enter image description here