0

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)
rsh52
  • 67
  • 7
  • Can you create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that we can work with? – markus Jun 05 '19 at 13:45
  • Hi @markus, added random sample data, it won't look pretty but it will produce the UCL/LCL I'm looking to correct. – rsh52 Jun 05 '19 at 14:01
  • 1
    If `p` is your plot, do you need `p$layers[[1]] <- NULL; p$layers <- c(p$layers, geom_step(data = p$data, aes(y = ucl), linetype = "dotted", col = "grey70")); p` ? – markus Jun 05 '19 at 14:21
  • I apologize, this actually worked exactly like I needed it to and taught me a lot about accessing the underlying layers of a `ggplot`, thank you. Once I work out the kinks I'll post it. – rsh52 Jun 05 '19 at 15:33

1 Answers1

0

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

rsh52
  • 67
  • 7