使用R将qicharts图转换为ggplot [英] Convert a qicharts plot to ggplot using R
问题描述
我的数据框看起来像这样
日期时间< - 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)
现在我使用qichart包中的qic函数并获取此图。
library(qicharts)
qic(FailRate_M1,
x = Datetime,
data = df1 ,
chart ='c',
runvals = TRUE,
cex = 1.2,
main ='测量失败率(M1)',
ylab ='MFR (%)',
xlab ='Datetime')
这张图可以使用ggplot绘图吗?或者可以将它转换为ggplot格式?请提供你的意见,并帮助我解决这个问题。
有很多功能都有自己定制的绘图方式,但我最好想看看我们是否可以将这些图转换为ggplot。
我试图做以下事情:
p1 < - qic FailRate_M1,
x = Datetime,
data = df1,
chart ='c',
runvals = TRUE,
cex = 1.2,
main ='测量失败率(M1)',
ylab ='MFR(%)',
xlab ='Datetime')
然后我尝试使用ggplot
library(ggplot2)
sp < - ggplot(p1,aes(x = Datetime,y = FailRate_M1))+
geom_point(size = 2.5)
sp
并获得以下错误错误:ggplot2不知道如何处理类qic的数据
我并不熟悉 qicharts :: qic
在做什么,但以下模仿了核心图形的元素与 ggplot2
:
library(ggplot2)
my_value < - min(df1 $ FailRate_M1)+ 6
ggplot(df1,aes(x = Datetime,y = FailRate_M1,group = 1))+
geom_line(color = (colorblue,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 =测量失败率(M1),
y =MFR(%))
一些注释可以帮助您理解:
x
是一个因子时,您需要使用 aes(group = 1)
code> ggplot()知道数据属于一个,应该是连接的。在这种情况下,使用一行代码。
geom_point
的多次调用。第一个将绘制所有点。第二种方法会将 df1 $ FailRate_M1> = 10
中的子集
数据以红色
颜色。可能不太明显的是,在红色
点下方有一个 lightgreen
点。
geom_hline
时,我将 yintercepts
与 c( )
函数。或者,您可以拨打 geom_hline
两次。
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 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:
- When
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. - Notice the multiple calls to
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. - On the call to
geom_hline
I am plotting multipleyintercepts
with thec()
function. Alternatively, you could callgeom_hline
twice.
这篇关于使用R将qicharts图转换为ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!