【问题标题】:control charts plotting all dates in R在 R 中绘制所有日期的控制图
【发布时间】:2018-06-06 15:27:37
【问题描述】:

我对 qic 图 - 控制图有疑问。 我的 x.axis 没有绘制我想要的所有日期。我将日期四舍五入为每 14 天一次,周期为 59 周。我想要所有这些都被绘制出来,但是我对此有一些问题,并且在网上找不到任何东西。然而,我是控制图的新手。

这里是一个例子,虽然不是原始数据,所以这个例子中的周数较少,但只要绘制了所有日期就没有关系。

再现数据:

df <- data.frame(x = rep(1:24, 4), 
ReportMonth = (rep(seq(as.Date('2014-1-1'),
                               length.out = 24,
                               by = 'month'),
                                4)),
             num = rbinom(4 * 24, 100, 0.5),
             denom = round(runif(4 * 24, 90, 110)),
             grp1 = rep(c('g', 'h'), each = 48),
             grp2 = rep(c('A', 'B'), each = 24))
df

和绘图

qic(x= ReportMonth,
y= num,
n= denom,
data=df, 
chart= "i",
x.format="%Y-%m-%d",
x.angle = 90,
y.expand = 40, # where to start y axis from
xlab = "Month",
ylab= "Value")

我已经尝试过 ggplot2,但我没有成功。

 library(ggplot2)
 library(plyr)

 p3.1 <- rename(p3, c("x" = "Date"))
 p3.1$Date<-as.Date(p3.1$x, format="%Y/%m/%d")

 plot4 <- ggplot(p3.1, aes(x = Date,y = y )) +
 geom_ribbon(ymin = p3.1$lcl, ymax = p3.1$ucl, alpha = 0.4) +   # fill = ""
 geom_line(colour = "blue", size = .75) + 
 geom_line(aes(Date, cl)) +
 geom_point(colour = "red" , fill = "red", size = 1.5) +
 #x.axis(1, p3$x, format(p3$x, "%Y-%m-%d"), cex.axis = 0.7)+
 ggtitle(label = "Readmissions within 30 days") +
 labs(x = NULL, y = NULL)+
 theme_minimal()+
 theme(axis.text.x = element_text(angle = 90, hjust = 1))

#aes(x = format(ActiveDate,"%Y-%m"), group = 1)) + geom_line(stat = "count") 
#+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

plot4

【问题讨论】:

  • 你能创建一个 MRE:link
  • 我用例子重新编辑了这个问题。谢谢!
  • 您想知道如何使用qicharts::qic()ggplot2::ggplot() 在控制图上绘制数据框df?我很困惑,因为您对问题的描述与您的示例不符。例如,您说“59 周”,但您的 df 是 24 个月(2014-01-01 到 2015-12-01)。您是否希望我们忽略描述并仅在控制图上绘制 df
  • 抱歉给您带来了困惑。我想出了一个例子,因为原始数据不是要举例说明的。因此,只要绘制了我示例中的所有日期,周数就无关紧要。如您所见,并非所有日期都使用 qic 函数绘制。因此,我跳到 ggplot 函数,相信我将能够绘制所有日期。但失败了。有什么帮助吗?

标签: r ggplot2 control-charts


【解决方案1】:

您可以绘制两个可能的值:numdenom。为简单起见,我将计算这两个值的百分比并绘制pct。 (但您当然可以选择绘制numdenom。)

此外,根据您的数据框df,您有四组值:

  • 第 1 组:grp1 = g,grp2 = A
  • 第 2 组:grp1 = h,grp2 = A
  • 第 3 组:grp1 = g,grp2 = B
  • 第 4 组:grp1 = h,grp2 = B

您遇到的部分问题是您需要分别绘制每个组,但是当您使用qicharts2::qic()ggplot2::ggplot() 绘制时,您不包括这些组。为此,您必须首先将 grp1grp2 组合成一个组 (grp)。

library(tidyverse)
library(qicharts2)

df_2 <- 
  df %>% 
  # calculate percent
  mutate(pct = round(num/denom, digits = 2)) %>%
  # collapse grp1 and grp2 to make single grp column
  unite(grp1, grp2, col = "grp")

head(df_2)
  x ReportMonth num denom grp  pct
1 1  2014-01-01  46   100 g_A 0.46
2 2  2014-02-01  54   105 g_A 0.51
3 3  2014-03-01  49   100 g_A 0.49
4 4  2014-04-01  56    94 g_A 0.60
5 5  2014-05-01  54   102 g_A 0.53
6 6  2014-06-01  48   106 g_A 0.45

在折线图(时间序列)上绘制多个组是完全可以的。

ggplot(df_2, aes(x = ReportMonth, y = pct, color = grp)) +
  geom_line() +
  scale_x_date(date_breaks = "2 months", date_labels = "%b '%y") +
  scale_y_continuous(labels = scales::percent) +
  theme_minimal()

但您应该在单个控制图上绘制多个组。控制图上的控制限基于单个系列(组)的历史值。如果您将所有四个组都绘制在同一个控制图上,您会得到四组控制限制,这会导致控制图非常混乱(几乎无法阅读/解释)。

您应该绘制四个控制图,每组一个。

df_2 %>% 
  # nested dataframe
  split(.$grp) %>% 
  # apply qic
  purrr::map(~ qicharts2::qic(
    ReportMonth, pct, 
    data = ., 
    chart = "i", # choose an appropriate control chart
    title = paste("Group:", unique(.$grp)),
    xlab = "ReportMonth",
    ylab = "pct"
    ))

编辑:

我在qicharts2::qic() 中找不到任何指定中断的参数(类似于ggplot 中的scale_x_date(breaks = ...) 函数)。请参阅参考手册here

但是,一种可能的解决方法是将日期变量转换为一个因子并改用它。这种方法的缺点是没有线连接点。

# Set levels for date variable -- ensure they are unique.
ReportMonth_levels <- format( unique(df_2$ReportMonth), "%b %y")

df_3 <- 
  df_2 %>% 
  # convert date variale to a factor with set levels
  mutate(ReportMonth = factor( format(ReportMonth, "%b %y"), levels = ReportMonth_levels))

df_3 %>% 
  qicharts2::qic(
    ReportMonth, pct, 
    data = ., 
    facets = ~ grp, # put all groups on one chart
    y.percent = TRUE,
    x.angle = 45,
    chart = "i", # choose an appropriate control chart
    xlab = "ReportMonth",
    ylab = "pct"
  )

【讨论】:

  • 注意:我在这里只展示四个控制图之一。上面的代码将生成四个单独的控制图,每个组一个。
  • 嘿乔丹,谢谢。我不确定你是否明白我想要什么。在您提供的地块上,我没有绘制所有我感兴趣的日期。我得到了所有这些 qic 图表没有问题,但未能绘制所有日期。显然,qic 函数只需要很少的日期并绘制它们。
  • 是的,但不是您拥有的数据框中的所有日期。我感兴趣的。
  • 我想我现在明白你的问题了。请参阅上面的编辑。
  • 非常感谢,乔丹。如果我找到任何答案,我会告诉你。然而,可能是 ggplot2 具有在这些图上放置一条线的功能。我真的希望那些创建图书馆(qicharts2)的人会考虑这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-10-05
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多