My dataframe looks like this
Datetime <- c("2015-09-29AM", "2015-09-29PM" ,"2015-09-30AM", "2015-09-30PM", "2015-10-01AM" ,"2015-10-01PM"
,"2015-10-02AM", "2015-10-02PM" ,"2015-10-03AM" ,"2015-10-03PM", "2015-10-04AM" ,"2015-10-04PM"
,"2015-10-05AM", "2015-10-05PM", "2015-10-06AM" ,"2015-10-06PM")
FailRate_M1 <- c(0.0000000,0.0000000,0.9615385,0.9009009,0.0000000,1.4492754,1.5151515,0.0000000,0.8849558,0.0000000,4.4444444,0.7142857
,0.0000000,10.3448276,0.0000000,0.0000000)
df1 <- data.frame(Datetime,FailRate_M1)
Now i use the qic function from the "qichart" package and obtain this plot.
library(qicharts)
qic(FailRate_M1,
x = Datetime,
data = df1,
chart = 'c',
runvals = TRUE,
cex = 1.2,
main = 'Measurement Fail Rate (M1)',
ylab = 'MFR (%)',
xlab = 'Datetime')
Can this plot be plotted using ggplot? or can it be converted to a ggplot format?. Kindly please provide your inputs and help me in solving this problem.
There are many functions that have thier own customized way of plotting but I would ideally like to see if we could convert those plots to ggplot.
I tried to do the following
p1<- qic(FailRate_M1,
x = Datetime,
data = df1,
chart = 'c',
runvals = TRUE,
cex = 1.2,
main = 'Measurement Fail Rate (M1)',
ylab = 'MFR (%)',
xlab = 'Datetime')
and then I try to use ggplot
library(ggplot2)
sp <- ggplot(p1, aes(x = Datetime, y = FailRate_M1))+
geom_point(size=2.5)
sp
and get the following error "Error: ggplot2 doesn't know how to deal with data of class qic"
I am not familiar with what what
qicharts::qic
is doing, but the following mimics the core elements of the graphic withggplot2
:A couple notes to aid your understanding:
x
is a factor, you need to useaes(group = 1)
so thatggplot()
knows the data "belong together" and should be "connected". In this case, with a line.geom_point
. The first will plot all of the points. The second will blot just thesubset
of data wheredf1$FailRate_M1 >= 10
with ared
color. What may not be obvious is that there is alightgreen
point underneath thisred
point.geom_hline
I am plotting multipleyintercepts
with thec()
function. Alternatively, you could callgeom_hline
twice.Building on Jason's answer with your p1 dataframe.
You can access the values returned by the qic function and use print.out =TRUE in the function call to see them in the console.
Updated answer using dplyr:
final plot
The intercept lines are no longer hard coded. I remove extraneous horizontal lines from all run/control charts, ggExtra's removeGrid and rotateTextX() are much easier (for me at least) to remember than equivalent ggplot2 syntax