使用R将气图转换为气图

时间:2021-08-29 20:14:51

My dataframe looks like this

我的dataframe是这样的

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.

现在我使用来自“qichart”包的qic函数来获取这个图。

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')

使用R将气图转换为气图

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.

可以用ggplot绘图吗?还是可以转换成ggplot格式?请提供您的意见,并帮助我解决这个问题。

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.

有很多函数都有自己定制的绘图方式,但我想看看能否将这些绘图转换成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

然后我尝试使用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"

得到下面的错误"错误:ggplot2不知道如何处理类qic的数据"

2 个解决方案

#1


1  

I am not familiar with what what qicharts::qic is doing, but the following mimics the core elements of the graphic with ggplot2:

我不太了解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 = "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 (%)")

使用R将气图转换为气图

A couple notes to aid your understanding:

以下是一些帮助你理解的笔记:

  • When 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.
  • 当x是一个因子时,您需要使用aes(group = 1),以便ggplot()知道数据“属于一起”,并且应该“连接”。在这种情况下,用一条线。
  • Notice the multiple calls to 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.
  • 注意到对地点的多次调用。第一个将绘制所有的点。第二种方法只会在df1$FailRate_M1 >= 10的数据子集上涂上红色。可能不明显的是在这个红点下面有一个浅绿色的点。
  • On the call to geom_hline I am plotting multiple yintercepts with the c() function. Alternatively, you could call geom_hline twice.
  • 在调用geom_hline时,我使用c()函数绘制多个yintercepts。或者,您可以两次调用geom_hline。

#2


2  

Building on Jason's answer with your p1 dataframe.

以Jason的回答为基础,使用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.

您可以访问qic函数返回的值并使用print。out =TRUE在函数调用中,在控制台中查看它们。

Updated answer using dplyr:

更新使用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

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

拦截线不再是硬编码。我从所有运行/控制图表中删除了无关的横线,与等效的ggplot2语法相比,ggExtra的removeGrid和rotateTextX()更容易记住(至少对我来说)

#1


1  

I am not familiar with what what qicharts::qic is doing, but the following mimics the core elements of the graphic with ggplot2:

我不太了解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 = "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 (%)")

使用R将气图转换为气图

A couple notes to aid your understanding:

以下是一些帮助你理解的笔记:

  • When 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.
  • 当x是一个因子时,您需要使用aes(group = 1),以便ggplot()知道数据“属于一起”,并且应该“连接”。在这种情况下,用一条线。
  • Notice the multiple calls to 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.
  • 注意到对地点的多次调用。第一个将绘制所有的点。第二种方法只会在df1$FailRate_M1 >= 10的数据子集上涂上红色。可能不明显的是在这个红点下面有一个浅绿色的点。
  • On the call to geom_hline I am plotting multiple yintercepts with the c() function. Alternatively, you could call geom_hline twice.
  • 在调用geom_hline时,我使用c()函数绘制多个yintercepts。或者,您可以两次调用geom_hline。

#2


2  

Building on Jason's answer with your p1 dataframe.

以Jason的回答为基础,使用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.

您可以访问qic函数返回的值并使用print。out =TRUE在函数调用中,在控制台中查看它们。

Updated answer using dplyr:

更新使用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

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

拦截线不再是硬编码。我从所有运行/控制图表中删除了无关的横线,与等效的ggplot2语法相比,ggExtra的removeGrid和rotateTextX()更容易记住(至少对我来说)