I have problems with qic charts - control charts. my x.axis does not plot all the dates I want. I rounded the dates to every 14 days, and the period is of 59 weeks. I want all of these plotted, yet I have issues around that and could not find anything on that online. Yet, I am new to the control charts.
Here is an example, not the original data though, so the number of weeks are less here in this example but it does not matter as long as all dates are plotted.
Reproducing the data:
df <- data.frame(x = rep(1:24, 4),
ReportMonth = (rep(seq(as.Date('2014-1-1'),
length.out = 24,
by = 'month'),
4)),
num = rbinom(4 * 24, 100, 0.5),
denom = round(runif(4 * 24, 90, 110)),
grp1 = rep(c('g', 'h'), each = 48),
grp2 = rep(c('A', 'B'), each = 24))
df
And plotting
qic(x= ReportMonth,
y= num,
n= denom,
data=df,
chart= "i",
x.format="%Y-%m-%d",
x.angle = 90,
y.expand = 40, # where to start y axis from
xlab = "Month",
ylab= "Value")
I have tried with ggplot2 yet, I have not succeeded.
library(ggplot2)
library(plyr)
p3.1 <- rename(p3, c("x" = "Date"))
p3.1$Date<-as.Date(p3.1$x, format="%Y/%m/%d")
plot4 <- ggplot(p3.1, aes(x = Date,y = y )) +
geom_ribbon(ymin = p3.1$lcl, ymax = p3.1$ucl, alpha = 0.4) + # fill = ""
geom_line(colour = "blue", size = .75) +
geom_line(aes(Date, cl)) +
geom_point(colour = "red" , fill = "red", size = 1.5) +
#x.axis(1, p3$x, format(p3$x, "%Y-%m-%d"), cex.axis = 0.7)+
ggtitle(label = "Readmissions within 30 days") +
labs(x = NULL, y = NULL)+
theme_minimal()+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
#aes(x = format(ActiveDate,"%Y-%m"), group = 1)) + geom_line(stat = "count")
#+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
plot4
You have two possible values to plot:
num
anddenom
. For simplicity sake I'm going to calculate the percent of these two values and plotpct
. (But you can certainly choose to plotnum
ordenom
instead.)Also, based on your data frame,
df
, you have four groups of values:Part of the problem you are having is you need to plot each group separately, but you are not including these groups when you plot with
qicharts2::qic()
orggplot2::ggplot()
. To do so you have to first combinegrp1
andgrp2
into one group (grp
).It is perfectly fine to plot multiple groups on a line chart (time series).
But you should not plot multiple groups on a single control chart. The control limits on a control chart are based on a single series (group's) historic values. If you plot all four groups on the same control chart you would get four sets of control limits which would make for a very confusing (almost impossible to read/interpret) control chart.
Instead you should plot four control charts, one for each group.
EDIT:
I could not find any parameter in
qicharts2::qic()
that specifies breaks (similar to thescale_x_date(breaks = ...)
function inggplot
). See reference manual here.However, a possible work around is to convert the date variable into a factor and use that instead. The downside to this approach is there is no line connecting the dots.