I want to know is their a way to automatically define Phase instead of manually defining and also how to change the names.
Example:
Instead of defining "newdata" in the below code, I want to know how we can state Phase automatically when we found some change in data. Also we have names as "Calibration data" and "New Data", I want to name this as "Phase1" or "Phase 2" or it may be based on user defined.
obj<- qcc(diameter[1:30,], type = "xbar",newdata= diameter[31:40,])
There are many ways to annotate plots. Since the qcc plot seems to be using the base plotting method, I wrote up this very simple example.
library(dplyr)
df <- data.frame(Site = c(rep("A", 8), rep("B", 4)),
X = 1:12, Value = rnorm(12))
df <- df %>% mutate(Phase = ifelse(Site != lag(Site) | is.na(lag(Site)),
paste0("Phase", Site), ""))
df
#> Site X Value Phase
#> 1 A 1 0.2878700 PhaseA
#> 2 A 2 1.8036211
#> 3 A 3 -1.9317990
#> 4 A 4 1.3148212
#> 5 A 5 -0.7595934
#> 6 A 6 0.9623186
#> 7 A 7 0.1178027
#> 8 A 8 1.6643182
#> 9 B 9 0.6364260 PhaseB
#> 10 B 10 -0.4310004
#> 11 B 11 0.6418500
#> 12 B 12 -0.4038598
plot(df$X, df$Value)
axis(3, at = df$X, labels = df$Phase, tick = FALSE)
Phases <- which(df$Phase != "")
abline(v = df[Phases, "X"], lty = 2)
Created on 2019-05-21 by the reprex package (v0.2.1)
There is also a text() function that could be useful. The ggplot2 package can also annotate plots in several ways. Without a concrete example, it is hard to be more specific.
Please post the data as part of a Reproducible Example and include calls to libraries() and code for making the plot as far as you know how. Thank you!
qiCharts2 uses ggplot2 for its plotting, so the base plotting method I demonstrated will not work. You can tell it uses ggplot because on the CRAN page for the package, it says it imports ggplot2. qiCharts2 also processes the data before plotting it. To make a flexible function for adding annotations, one would have to understand the details of that. Below I demonstrate a very manual solution for adding a couple of annotations to the plot. After making the PlotOOC object, I extract the data from it, add a Phase column and offset the y values by 120 and then use geom_text from ggplot2 to add the annotations. Note that I also modified your data so that the dates have leading zeros for the month and day. Those dates will not plot in the correct order without doing that because they are imported as characters, not as dates.
I am sure the plot is not exactly how you want it and that the process I used is confusing. Keep in mind that functions from packages like qiCharts2 are intended to be used "as is" and that modifying the output is likely to require digging into the code to understand the details of the data processing.
library(qicharts2)
#> Warning: package 'qicharts2' was built under R version 3.5.3
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
df2 <- read.table("c:/users/fjcc/Documents/R/Play/QIChart.txt",
header = TRUE, sep = " ")
df2
#> Parts Date Supplier Built_Units Failed_Units DPPM Phase
#> 1 1 01/01/2018 A 7000 4 571.4286 Phase1
#> 2 2 01/02/2018 A 10000 5 500.0000
#> 3 3 01/03/2018 A 8000 6 750.0000
#> 4 4 01/04/2018 B 12000 7 583.3333
#> 5 5 01/05/2018 C 8880 8 900.9009
#> 6 6 01/06/2018 D 6000 2 333.3333 Phase2
#> 7 7 01/07/2018 E 22000 20 909.0909
#> 8 8 01/08/2018 E 20000 10 500.0000
#> 9 9 01/09/2018 T 33000 12 363.6364
#> 10 10 01/10/2018 T 48000 18 375.0000
PlotOOC<-qic(df2$Failed_Units,
n=df2$Built_Units,
x=df2$Date,
x.angle=60,
data=df2,
# exclude = TRUE,
# facets = tmp6$Week,
show.labels = TRUE,
multiply = 1000000,
chart='up',
point.size = 3,
scales = 'free_x',
# ?cut.POSIXt
x.period=df2$Date,
x.format="%Y-%m-%d",
title=paste('Control chart for',df2$Parts, "and",df2$Supplier),
xlab='Week',
ylab='Defective parts per million'
# print.summary = TRUE
)
PlotDat <- PlotOOC$data
PlotDat <- PlotDat %>% mutate(Phase = df2$Phase, y = y + 120)
PlotOOC + geom_text(data = PlotDat, mapping = aes(label = Phase))
I am clear that using geom_text we can include phases for plots. I need one modification, how can we make the lines to be vertical, it should show phase based on a week.
Example:
1st phase should be in Jan 2018 and 2nd on be June 2018.
There is a geom_vline in ggplot2 for adding vertical lines. I had to make the dates into real dates in order to make it work and I set x.period to "month".
library(qicharts2)
#> Warning: package 'qicharts2' was built under R version 3.5.3
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
df2 <- read.table("c:/users/fxcampos/Documents/R/Play/QIChart.txt",
header = TRUE, sep = " ")
df2 <- df2 %>% mutate(Date = dmy(Date))
df2
#> Parts Date Supplier Built_Units Failed_Units DPPM Phase
#> 1 1 2018-01-01 A 7000 4 571.4286 Phase1
#> 2 2 2018-02-01 A 10000 5 500.0000
#> 3 3 2018-03-01 A 8000 6 750.0000
#> 4 4 2018-04-01 B 12000 7 583.3333
#> 5 5 2018-05-01 C 8880 8 900.9009
#> 6 6 2018-06-01 D 6000 2 333.3333 Phase2
#> 7 7 2018-07-01 E 22000 20 909.0909
#> 8 8 2018-08-01 E 20000 10 500.0000
#> 9 9 2018-09-01 T 33000 12 363.6364
#> 10 10 2018-10-01 T 48000 18 375.0000
PlotOOC<-qic(df2$Failed_Units,
n=df2$Built_Units,
x=df2$Date,
x.angle=60,
data=df2,
# exclude = TRUE,
# facets = tmp6$Week,
show.labels = TRUE,
multiply = 1000000,
chart='up',
point.size = 3,
scales = 'free_x',
# ?cut.POSIXt
x.period="month",
x.format="%Y-%m-%d",
title=paste('Control chart for',df2$Parts, "and",df2$Supplier),
xlab='Week',
ylab='Defective parts per million'
# print.summary = TRUE
)
PlotDat <- PlotOOC$data
PlotDat <- PlotDat %>% mutate(Phase = df2$Phase, y = y + 120) %>%
filter(Phase != "")
PlotOOC + geom_text(data = PlotDat, mapping = aes(label = Phase)) +
geom_vline(data = PlotDat, mapping = aes(xintercept = x), linetype = 2)