Reputation: 2267
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"
Upvotes: 4
Views: 1246
Reputation: 320
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:
library(dplyr)
library(ggplot2)
library(ggExtra)# optional for plot tidying
df2 <- data.frame(p1$labels,p1$y,p1$cl,p1$ucl) %>%
dplyr::rename(y = p1.y,
Datetime = p1.labels,
cl = p1.cl,
ucl= p1.ucl)
p <- ggplot(df2, aes(x = Datetime, y = y, group = 1)) +
theme_minimal() +
geom_line(color = "steelblue", size = 1) +
geom_point(color = "steelblue", size = 3) +
geom_point(data = subset(df2, FailRate_M1 >= 10), color = "red", size = 4) +
geom_hline(aes(yintercept = cl)) +
geom_hline(aes(yintercept = ucl)) +
labs(title = "Measurement Fail Rate (M1)",
y = "MFR (%)")
p <- p + removeGrid() + rotateTextX() #from ggExtra,personal preference
p
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
Upvotes: 3
Reputation: 20463
I am not familiar with what what qicharts::qic
is doing, but the following mimics the core elements of the graphic with ggplot2
:
library(ggplot2)
my_value <- min(df1$FailRate_M1) + 6
ggplot(df1, aes(x = Datetime, y = FailRate_M1, group = 1)) +
geom_line(color = "steelblue", size = 1) +
geom_point(color = "lightgreen", size = 3) +
geom_point(data = subset(df1, FailRate_M1 >= 10), color = "red", size = 4) +
geom_hline(aes(yintercept = c(1.3, 4.8))) +
geom_hline(aes(yintercept = my_value), linetype = 2) +
labs(title = "Measurement Fail Rate (M1)",
y = "MFR (%)")
A couple notes to aid your understanding:
x
is a factor, you need to use aes(group = 1)
so that ggplot()
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 the subset
of data where df1$FailRate_M1 >= 10
with a red
color. What may not be obvious is that there is a lightgreen
point underneath this red
point.geom_hline
I am plotting multiple yintercepts
with the c()
function. Alternatively, you could call geom_hline
twice.Upvotes: 2