使用 ggplot2 facet wrap R 控制图表
Control Charts Using ggplot2 facet wrap R
我有下图:
这是使用 dplyr group_by
和 summarise
函数创建的 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")
我想用控制图替换它,我看过 and here 但似乎无法锻炼如何在使用 facet_wrap
时合并
我一直在玩qcc和qicharts,比如:
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')
任何带有 ggplot2 facet_wrap
的指针或代码示例将不胜感激
你可以试试:
# 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")
您可以使用 "qicharts" 包中的 'tcc' 函数。
函数参数包括 g1 和 g2(用于定义构面的分组向量)。
来自包函数帮助:
"tcc() is a wrapper function for ggplot2() that makes multivariate run and control charts. It takes up to two grouping variables for multidimensional trellis plots"。
对于 facet_grid() 样式图,您需要同时指定 g1 和 g2,否则对于 facet_wrap() 只需指定 g1。
以下是函数帮助中的一些稍作修改的示例,以及一些结果图:
library(qicharts)
# Build data frame for examples
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))
# Run chart with two grouping variables
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, main = "trellis run chart")
# C chart
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'c',
main ="trellis C chart")
tcc(num, denom, ReportMonth, g1 = grp1, data = df, chart = 'p',
main ="trellis P chart , 1 facet")
# P chart
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'p',
main ="trellis P chart")
# P chart with baseline fixed to the first 9 data points
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'p', freeze = 9,
main = "trellis P chart with limits fixed to first 9 data points")
# U chart with two breaks and summary output
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'u',
breaks = c(9, 15), main =" trellis U chart with multiple breaks",
print.summary = TRUE)
请注意,tcc 函数可以生成 运行 图表以及控制图表。
您还可以使用其他 ggplot2 代码修改绘图外观。
或者看看 ggQC 包,它也可以轻松生成多面图 ggQC。
终于发布了新版qicharts2qicharts2
qic 函数中有一个 facets 参数(新包中不存在 tcc 函数),因此您的代码将具有
facets = grp1 ~ grp2
在函数调用中。
我有下图:
这是使用 dplyr group_by
和 summarise
函数创建的 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")
我想用控制图替换它,我看过 facet_wrap
我一直在玩qcc和qicharts,比如:
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')
任何带有 ggplot2 facet_wrap
的指针或代码示例将不胜感激
你可以试试:
# 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")
您可以使用 "qicharts" 包中的 'tcc' 函数。
函数参数包括 g1 和 g2(用于定义构面的分组向量)。
来自包函数帮助:
"tcc() is a wrapper function for ggplot2() that makes multivariate run and control charts. It takes up to two grouping variables for multidimensional trellis plots"。
对于 facet_grid() 样式图,您需要同时指定 g1 和 g2,否则对于 facet_wrap() 只需指定 g1。
以下是函数帮助中的一些稍作修改的示例,以及一些结果图:
library(qicharts)
# Build data frame for examples
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))
# Run chart with two grouping variables
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, main = "trellis run chart")
# C chart
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'c',
main ="trellis C chart")
tcc(num, denom, ReportMonth, g1 = grp1, data = df, chart = 'p',
main ="trellis P chart , 1 facet")
# P chart
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'p',
main ="trellis P chart")
# P chart with baseline fixed to the first 9 data points
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'p', freeze = 9,
main = "trellis P chart with limits fixed to first 9 data points")
# U chart with two breaks and summary output
tcc(num, denom, ReportMonth, g1 = grp1, g2 = grp2, data = df, chart = 'u',
breaks = c(9, 15), main =" trellis U chart with multiple breaks",
print.summary = TRUE)
请注意,tcc 函数可以生成 运行 图表以及控制图表。
您还可以使用其他 ggplot2 代码修改绘图外观。
或者看看 ggQC 包,它也可以轻松生成多面图 ggQC。
终于发布了新版qicharts2qicharts2
qic 函数中有一个 facets 参数(新包中不存在 tcc 函数),因此您的代码将具有
facets = grp1 ~ grp2
在函数调用中。