【问题标题】:ggplot2 stacked bar plotsggplot2堆积条形图
【发布时间】:2014-07-06 14:04:41
【问题描述】:

我正在尝试可视化错误跟踪系统的统计数据。

我想做的是大致了解收到的错误通知单和已修复的错误通知单,我认为条形图会是一个很好的解决方案。

我买了 Hadley Wickham 的书“用于数据分析的 ggplot2 优雅图形”,我正在尝试了解 geoms 和 stats 的工作原理,但我认为如果没有额外的支持,我将需要比我能花更多的时间研究它。

如果您能帮助我根据数据表了解进出票的概览,那就太好了(不幸的是,在问题中似乎不可能附加 csv)。

id external    in.date      fixed in.cw fixed.cw
 1        x 01.11.2013 15.11.2013  1344     1346
 2          07.11.2013             1345     <NA>
 3        x 15.11.2013             1346     <NA>
 4          01.11.2013 15.11.2013  1344     1346
 5        x 07.11.2013 20.11.2014  1345     1447
 6          15.11.2013             1346     <NA>
 7        x 01.11.2013             1344     <NA>
 8          07.11.2013 05.01.2014  1345     1402
 9        x 15.11.2013 05.01.2014  1346     1402
10          01.11.2013 05.01.2014  1344     1402
11        x 07.11.2013             1345     <NA>
12          15.11.2013             1346     <NA>
13        x 01.11.2013 01.03.2014  1344     1409
14          07.11.2013 01.04.2014  1345     1414
15        x 15.11.2013             1346     <NA>
16          01.11.2013 01.05.2014  1344     1418

我认为如果将固定票添加到情节的附加层中,它将提供一个很好的概述。

是否也可以定义一个透明度值,以便叠加的条形图仍然可见?

在我标记的数据中,还有一些作为外部工单的条目,我想以某种不同的方式对其进行可视化,以通过用模式填充外部工单的计数来显示与记者来源的关系。

稍后我还想包括门票的优先级,但这将是除了添加预测、图例和其他可能之外的后续步骤之一。

这是我到目前为止所做的:

p <- ggplot(data=table) + stat_bin( aes(x=factor(in.cw), y=..count.., fill = factor(external)))  
p +  stat_bin(data=table, aes(x=factor(fixed.cw), y=..count..))#, fill = factor(external)) 

我觉得一开始还不错:)

您能否告诉我如何获得第二层,其中包含填充了颜色的 fixed.cw 票证,并告诉我如何为它定义线型?

这里是所用数据的转储:

> dput(table)
structure(list(id = 1:16, external = c("x", "", "x", "", "x", 
"", "x", "", "x", "", "x", "", "x", "", "x", ""), in.date = c("01.11.2013", 
"07.11.2013", "15.11.2013", "01.11.2013", "07.11.2013", "15.11.2013", 
"01.11.2013", "07.11.2013", "15.11.2013", "01.11.2013", "07.11.2013", 
"15.11.2013", "01.11.2013", "07.11.2013", "15.11.2013", "01.11.2013"
), fixed = c("15.11.2013", "", "", "15.11.2013", "20.11.2014", 
"", "", "05.01.2014", "05.01.2014", "05.01.2014", "", "", "01.03.2014", 
"01.04.2014", "", "01.05.2014"), in.cw = c("1344", "1345", "1346", 
"1344", "1345", "1346", "1344", "1345", "1346", "1344", "1345", 
"1346", "1344", "1345", "1346", "1344"), fixed.cw = c("1346", 
NA, NA, "1346", "1447", NA, NA, "1402", "1402", "1402", NA, NA, 
"1409", "1414", NA, "1418")), .Names = c("id", "external", "in.date", 
"fixed", "in.cw", "fixed.cw"), row.names = c(NA, -16L), class = "data.frame")

这只是我创建的测试数据。

“外部”标记客户创建的条目。

'in.date' 定义了创建日期。

'fixed' 定义关闭错误报告的日期。

'in.cw' 和 'fixed.cw' 表示创建/关闭报告的年份和 callendar 周。

一开始,我尝试创建一个图表,概述报告与已关闭的报告。理想情况下,通过将外部与其他条目分开。 基于 in.cw 和 fixed.cw 值的预测也会很好。 问候 瓦西里

问候,
瓦西里

【问题讨论】:

  • 首先,在您的数据框上调用 dput() 并粘贴输出以便于共享。其次,您可能想解释一下您的列是什么以及您具体要达到什么目的——例如,您需要多少个图表来显示信息;什么细分有用?
  • 我不知道您在数据中的频率类型,但您可能想按天、周月或其他方式汇总新病例?

标签: r ggplot2


【解决方案1】:

我仍然不知道您正在寻找什么样的组织,但这应该可以帮助您入门并假设您想在绘图之前汇总数据。

library(ggplot2)
library(plyr)

 test<-structure(list(id = 1:16, external = c("x", "", "x", "", "x", 
 "", "x", "", "x", "", "x", "", "x", "", "x", ""), in.date = c("01.11.2013", 
 "07.11.2013", "15.11.2013", "01.11.2013", "07.11.2013", "15.11.2013", 
 "01.11.2013", "07.11.2013", "15.11.2013", "01.11.2013", "07.11.2013", 
 "15.11.2013", "01.11.2013", "07.11.2013", "15.11.2013", "01.11.2013"
 ), fixed = c("15.11.2013", "", "", "15.11.2013", "20.11.2014", 
 "", "", "05.01.2014", "05.01.2014", "05.01.2014", "", "", "01.03.2014", 
 "01.04.2014", "", "01.05.2014"), in.cw = c("1344", "1345", "1346", 
 "1344", "1345", "1346", "1344", "1345", "1346", "1344", "1345", 
 "1346", "1344", "1345", "1346", "1344"), fixed.cw = c("1346", 
 NA, NA, "1346", "1447", NA, NA, "1402", "1402", "1402", NA, NA, 
 "1409", "1414", NA, "1418")), .Names = c("id", "external", "in.date", 
 "fixed", "in.cw", "fixed.cw"), row.names = c(NA, -16L), class = "data.frame")

## code external/interlal variable
test$origin<-ifelse(test$external=='x','external','internal')

## store dates as actual date objects
test$in.date<-as.Date(test$in.date,format='%d.%m.%Y')
test$fixed<-as.Date(test$fixed,format='%d.%m.%Y')

## calculate process time in days for completed records
test$fixtime<-test$fixed-test$in.date

## discretize process time into groups for summary purposes
test$fixtime_categories<-cut(as.numeric(test$fixtime),breaks=c(seq(1,100,40),Inf))


## summarize data by categorized process time and whether origin=external
summary_data <- ddply(test,
                      .(fixtime_categories,origin), summarise, 
                      records = length(id)) 

## plotting
ggplot(summary_data)+ 
  geom_bar(aes(x=fixtime_categories,y=records),stat="identity") +#,position="fill") +
  facet_wrap(~origin)+
  ggtitle('Process time by (exernal) filing status')

这会产生下面的图表,通过完成它们所花费的时间来显示案例的数量(这里,NA 是那些尚未完成的;这些可以被忽略或包括在内,具体取决于用例)。左侧面板仅是外壳;右侧面板内部的。

【讨论】: