合并qicharts和ggplot2以生成ggplot控制图

时间:2017-04-27 19:09:01

标签: r ggplot2 charts

好的,这将会持续很长时间,但我尝试做的基本总结是结合(至少本质/不一定是文字组合)我创建的图表使用ggplot2和我使用qicharts创建的控制图表。

首先,这里是我用于图表的数据的基本结构。 (底部的可重复数据)

| FileName | Version | Category  | Value | TestNum | RepNum | Case  |
|----------|---------|-----------|-------|---------|--------|-------|
| File1    | 1.0.1   | Category1 |   100 |       1 |      1 | Case1 |
| File1    | 1.0.1   | Category2 |   200 |       1 |      1 | Case1 |
| File1    | 1.0.1   | Category1 |   101 |       1 |      2 | Case1 |
| File1    | 1.0.1   | Category2 |   199 |       1 |      2 | Case1 |
| File2    | 1.0.1   | Category1 |    50 |       1 |      1 | Case1 |
| File2    | 1.0.1   | Category2 |    75 |       1 |      1 | Case1 |
| File2    | 1.0.1   | Category1 |    53 |       1 |      2 | Case1 |
| File2    | 1.0.1   | Category2 |    80 |       1 |      2 | Case1 |

*在实际的数据集中,TestNum会与下一个版本一起计算,并且有多个" Cases"。请参阅下面的可重复示例

这里是如何创建ggplot2图表的:

versions<-unique(df[order(df$TestNum), ][,2])
df$Version<-factor(df$Version, levels = versions)

g <- ggplot(subset(df, FileName=="File1" & Case=="Case1"), aes(x=Version, y=Value, group=FileName))
g <- g + geom_line(size=.25) + geom_point(size=1.2, colour='red') +
  ggtitle(expression(atop("TITLE", atop(italic("SUBTITLE"), "")))) +
  facet_grid(Category ~ ., scales="free", space="fixed", labeller=label_value) +
  xlab("version") + ylab("Metric Value") +
  expand_limits(y=0) +
  theme(axis.text.x=element_text(angle=45, vjust=1, hjust=1)) + 
  theme(axis.title.y=element_text(vjust=1)) + 
  theme(plot.margin=unit(c(0,0,0,0),"mm"))
g

它看起来像这样: ggplot2 chart

以下是控制图的生成方式:

subdf <- subset(df, FileName=="File1" & Category=="Category1")

qic(Value, 
    x = Version,
    data = subdf,
    chart = 'c',
    main = 'File1 - Category1',
    ylab = 'Category1',
    xlab = 'Version')

它看起来像这样: cchart

我真正想做的是将控制图线添加到原始ggplot2图表的各个方面。现在,当我可以将每个类别的控制图组合成一个数字时,我必须有多次数字才能获得控制图。

数据集

显示需要考虑的所有内容相当大

FileName <- c("File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File1", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2", "File2")
Version <- c("1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.1", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.2", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3", "1.0.3")
Category <- c("Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3","Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3", "Category1", "Category1", "Category1", "Category2", "Category2", "Category2", "Category3", "Category3", "Category3")
Value <- c(194,194,194,17,17,17,355,355,355,194,194,194,16,16,16,373,373,373,160,160,160,17,17,17,355,355,355,160,160,160,16,16,16,373,373,373,160,160,160,17,17,17,355,355,355,160,160,160,16,16,16,373,373,373,1918,1918,1918,17,17,17,355,355,355,1918,1918,1918,16,16,16,373,373,373,1918,1918,1918,17,17,17,355,355,355,1918,1918,1918,16,16,16,373,373,373,1918,1918,1918,17,17,17,355,355,355,1918,1918,1918,16,16,16,373,373,373)
TestNum <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
RepNum <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
Case <- c("Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case1", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2", "Case2")
df <- data.frame(FileName,Version,Category,Value,TestNum,RepNum,Case)

在图表代码之前加载它,您应该能够重现所有这些数字。对不起,如果它有点野蛮,但我不确定如何以更简单的方式完成模式。 (也许这是另一天的问题。)

1 个答案:

答案 0 :(得分:0)

qicharts2可以为您处理分面,只需在qic函数中使用“facets”参数即可。另请注意,该软件包现在使用ggplot2,因此您可以使用其他ggplot2命令修改图形。

使用原始df,您可以尝试

Calls to and from your free trial phone number will play a short trial message before your TwiML runs.

没有完全测试,(在我的平板电脑上),但这应该给你一个想法。 原始的qicharts包使用分组参数来创建构面(g1和g2),新版本更直观。

有关其他示例,请参阅我的回答control charts ggplot2 and facet wrap