我有以下图表:
这是使用带有group_by
的dplyr 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")
我想用控制图替换它,我看了here和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
的指针或代码示例都将受到高度赞赏
答案 0 :(得分:1)
您可以尝试:
# 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")
答案 1 :(得分:1)
您可以使用&#39; tcc&#39;功能在&#34; qicharts&#34;包。
在函数参数中有g1和g2(用于定义构面的分组向量)。
从包功能帮助:
&#34; tcc()是ggplot2()的包装函数,它可以生成多变量运行和控制图表。多维网格图最多需要两个分组变量&#34;。
对于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包,它也可以轻松生成多面图{。{3}}。
最后,qicharts2 ggQC的新版本已经发布。
qic函数中有一个facets参数(tcc函数在新包中不存在),所以你的代码会有
facets = grp1 ~ grp2
在函数调用中。