2

I can create quality control charts with the qicharts2 package.

library(tidyverse) library(qicharts2) (plot1 <- qic(age, data = tail(cabg, 100), chart = 'i', ylab = 'Years', xlab = 'Patient #' ) ) p1 <- plot1$data 

Then I can customize the charts.

(plot2 <- ggplot(p1, aes(x, y)) + geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "black", alpha = 0.05) + geom_line(color = "black", size = 1) + geom_line(aes(x, cl)) + geom_point(color = "black" , fill = "black", size = 2) + geom_point(data = p1 %>% filter(sigma.signal == TRUE), color = "red", size = 2) + ggtitle(label = NULL) + labs(x = NULL, y = NULL) + scale_y_continuous(breaks = seq(0, 100, by = 10)) + coord_cartesian(ylim = c(0, 100)) + theme_bw() + theme( text = element_text(size = 18), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6), axis.text.y = NULL, panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.text.x = element_text(size = 14, color = "black", angle = 0)) ) 

Using the part argument, in my qichart, causes it to split at the specified part point(s).

(plot3 <- qic(age, data = tail(cabg, 100), chart = 'i', part = c(70, 85), ylab = 'Years', xlab = 'Patient #' ) ) p3 <- plot3$data 

What do I need to add to my customized ggplot2 syntax, below, to get it to part in the same manner? What I've got does everything, EXCEPT, it doesn't part like in the syntax directly above.

(plot4 <- ggplot(p3, aes(x, y)) + geom_ribbon(ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) + geom_line(color = "black", size = 1) + geom_line(aes(x, cl)) + geom_point(color = "black" , fill = "black", size = 2) + geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) + ggtitle(label = NULL) + labs(x = NULL, y = NULL) + scale_y_continuous(breaks = seq(0, 100, by = 10)) + coord_cartesian(ylim = c(0, 100)) + theme_bw() + theme( text = element_text(size = 18), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6), axis.text.y = NULL, panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.text.x = element_text(size = 14, color = "black", angle = 0)) ) 

1 Answer 1

2

Is the following plot what you are looking for?

If so, what I used is group= in the aesthetics of geom_ribbon and geom_line

enter image description here

(plot4 <- ggplot(p3, aes(x, y)) + geom_ribbon(aes(group=cut(p3$x,c(0,70,85,max(p3$x)))),ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) + geom_line(color = "black", size = 1, aes(group=cut(p3$x,c(0,70,85,max(p3$x))))) + geom_line(aes(x, cl, group=cut(p3$x,c(0,70,85,max(p3$x))))) + geom_point(color = "black" , fill = "black", size = 2) + geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) + ggtitle(label = NULL) + labs(x = NULL, y = NULL) + scale_y_continuous(breaks = seq(0, 100, by = 10)) + coord_cartesian(ylim = c(0, 100)) + theme_bw() + theme( text = element_text(size = 18), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6), axis.text.y = NULL, panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.text.x = element_text(size = 14, color = "black", angle = 0))) 
Sign up to request clarification or add additional context in comments.

3 Comments

Very close. I'd also like to see the line broken. For example - the second red dot would be the last point of the first line. It wouldn't connect to the second line, as it currently does. How can we implement that? And any way to avoid writing that function at the beginning of your code. Maybe not?
post edited: I added group= to the geom_line of the line you mentioned. I also removed the extra line before the code and added it every time we need the "group" argument. I used "cut" this time to split the age into 3 groups, instead of if/else to make it shorter: cut(p3$x,c(0,70,85,max(p3$x))
Hey there @slackinator , would you consider accepting as an answer? Tks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.