在 R 中绘制所有日期的控制图
control charts plotting all dates in R
我对 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
您有两个可能的值可以绘制:num
和 denom
。为简单起见,我将计算这两个值的百分比并绘制 pct
。 (但您当然可以选择绘制 num
或 denom
。)
此外,根据您的数据框 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()
绘制时不包括这些组。为此,您必须首先将 grp1
和 grp2
合并为一组 (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()
但是您应该不在单个控制图上绘制多个组。控制图上的控制限基于单个系列(组)的历史值。如果您将所有四个组绘制在同一个控制图上,您将得到四组控制限制,这会使控制图非常混乱(几乎不可能read/interpret)。
相反,您应该绘制四张控制图,每组一张。
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 图 - 控制图有疑问。 我的 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
您有两个可能的值可以绘制:num
和 denom
。为简单起见,我将计算这两个值的百分比并绘制 pct
。 (但您当然可以选择绘制 num
或 denom
。)
此外,根据您的数据框 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()
绘制时不包括这些组。为此,您必须首先将 grp1
和 grp2
合并为一组 (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()
但是您应该不在单个控制图上绘制多个组。控制图上的控制限基于单个系列(组)的历史值。如果您将所有四个组绘制在同一个控制图上,您将得到四组控制限制,这会使控制图非常混乱(几乎不可能read/interpret)。
相反,您应该绘制四张控制图,每组一张。
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"
)