使用bupaR绘制吞吐量时间图时遇difftime类rescale方法报错
解决bupaR中throughput_time绘图时的difftime类rescale错误
问题重现
使用bupaR内置的patients数据集时,以下代码可正常绘制吞吐量时间图:
library(bupaR) pat <- bupaR::simple_eventlog(eventlog = patients, case_id = ".order", activity_id = "handling_id", timestamp = "time" ) pat %>% throughput_time("case") %>% plot()
但使用自定义的po59事件日志执行相同操作时:
po59 <- structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689", "WC4121068", "WC4120667", "WC4120666", "WC4120667", "WC4121068", "WC4120667", "WC4121068", "WC4121068", "WC4120666", "WC4120721", "WC4120666", "WC4120666"), lifecycle = c(110, 110, 110, 110, 120, 110, 130, 120, 10, 130, 10, 120, 120, 130, 10), action = c("WC4120721-CN354877", "WC4120667-CN354878", "WC4120689-CN356752", "WC4121068-CN301950", "WC4120667-CSW310", "WC4120666-CN354878", "WC4120667-CSW308", "WC4121068-CSW303", "WC4120667-CSW309", "WC4121068-CSW308", "WC4121068-CSW309", "WC4120666-CSW310", "WC4120721-CSW310", "WC4120666-CSW308", "WC4120666-CSW309" ), activity = c("Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "BBH-1&2", "Forged Wire, Medium (Sport)", "TCE Cleaning", "SOLO Oil", "Tempering", "TCE Cleaning", "Tempering", "BBH-1&2", "BBH-1&2", "TCE Cleaning", "Tempering"), resource = c("3419", "3216", "3409", "3201", "C3-100", "3216", "C3-080", "C3-030", "C3-090", "C3-080", "C3-090", "C3-100", "C3-100", "C3-080", "C3-090"), timestamp = structure(c(-62042010952, -62010323872, -61884118792, -61821087232, -61821025132, -61789534972, -61789520332, -61789494952, -61757940232, -61757925652, -61757895952, -61694827552, -61694808292, -61663337152, -61663307452), tzone = "Europe/Istanbul", class = c("POSIXct", "POSIXt")), .order = 1:15), row.names = c(NA, -15L), class = c("eventlog", "log", "tbl_df", "tbl", "data.frame"), case_id = "case_id", activity_id = "activity", activity_instance_id = "action", lifecycle_id = "lifecycle", resource_id = "resource", timestamp = "timestamp") po59 %>% throughput_time("case") %>% plot()
会触发以下错误:
Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "difftime"
问题原因
throughput_time("case")针对自定义数据集返回的吞吐量时间是difftime类型,而默认的plot函数尝试对该类型调用rescale方法,但目前没有针对difftime类的rescale实现。而patients数据集的时间计算后会自动转换为数值型(如秒数),因此可以正常绘图。
解决方法
方案1:将difftime转换为数值型后绘图
把difftime类型转换为具体的数值单位(如天、秒),再调用绘图函数:
library(bupaR) po59 %>% throughput_time("case") %>% # 转换为天为单位的数值,也可根据需求选择"secs"/"hours"等单位 mutate(throughput_time = as.numeric(throughput_time, units = "days")) %>% plot()
方案2:手动绘制自定义图形
如果需要更灵活的绘图控制,可以用ggplot2手动实现箱线图(和默认plot逻辑一致):
library(bupaR) library(ggplot2) tt_data <- po59 %>% throughput_time("case") %>% mutate(throughput_time_days = as.numeric(throughput_time, units = "days")) ggplot(tt_data, aes(x = factor(1), y = throughput_time_days)) + geom_boxplot(width = 0.5) + labs(title = "Case Throughput Time", y = "Throughput Time (days)", x = "") + theme_minimal()
内容的提问来源于stack exchange,提问作者firmo23
相关产品推荐
相关产品推荐

