使用ggplot2 facet wrap R的控制图 [英] Control Charts Using ggplot2 facet wrap R

查看:50
本文介绍了使用ggplot2 facet wrap R的控制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图形:

这是使用dplyr group_by summarise 函数以及 ggplot2 创建的:

 坡度%>%头(12)%&%;%inner_join(word_month_counts,通过="word")%>%mutate(word = reorder(word,-estimate))%&%;%ggplot(aes(month,prop_per_month,color = word))+geom_line(show.legend = FALSE,lwd = 1.3)+geom_smooth(se = FALSE,lty = 2)+facet_wrap(〜word,scales ="free_y") 

我想将其替换为控制图,并且已经查看了

I have the following graph:

this was created using the dplyr group_by and summarise functions with ggplot2:

slopes %>%
   head(12) %>%
  inner_join(word_month_counts, by = "word") %>%
  mutate(word = reorder(word, -estimate)) %>%
  ggplot(aes(month, prop_per_month, color = word)) +
  geom_line(show.legend = FALSE,lwd=1.3) +
  geom_smooth(se=FALSE,lty=2)+
  facet_wrap(~ word, scales = "free_y") 

I want to replace this with control charts and I have looked here and here but can't seem to workout how to incorporate when using facet_wrap

I have been playing with the qcc and qicharts like:

library(qicharts)

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(5045,4350,4350,3975,4290,4430,4485,4285,3980,3925,3645,3760,3300,3685,3463,5200)

df1 <- data.frame(Datetime,FailRate_M1)

qic(FailRate_M1,               
    x        = Datetime,              
    data     = df1,                  
    chart    = 'c',
    runvals  = TRUE,               
    cex      = 1.2,
    main     = 'Measurement Fail Rate (M1)', 
    ylab     = 'MFR (%)',          
    xlab     = 'Datetime')

Any pointers or code example with ggplot2 facet_wrap would be highly appreciated

解决方案

You can try:

# as you provided no reproducible example, I added afactor gr for the facet.
df1 <- data.frame(Datetime,FailRate_M1, gr=gl(2,8) )

# calculate the cl, ucl and lcl per group
df2 <- lapply(split(df1, df1$gr), function(data){
  a <- qic(FailRate_M1,               
           x        = Datetime,              
           data     = data,                  
           chart    = 'c',
           runvals  = TRUE)
  cbind.data.frame(ucl=a$ucl,cl= a$cl, lcl= a$lcl)
})

# the final data.frame  
df3 <- cbind(df1, do.call(rbind,df2))  

# and the final plot
ggplot(df3, aes(x=Datetime, y=FailRate_M1, group=gr)) + 
  geom_line(show.legend = FALSE,lwd=1.3) +
  geom_line(aes(x=Datetime, y=ucl)) +
  geom_line(aes(x=Datetime, y=cl)) +
  geom_line(aes(x=Datetime, y=lcl)) +
  facet_wrap(~ gr, scales = "free_x") 

这篇关于使用ggplot2 facet wrap R的控制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆