在R中创建水平条形图以显示活动顺序

时间:2017-11-14 05:36:26

标签: r ggplot2 plotly ggplotly visnetwork

数据集“患者”是访问诊所并接受治疗的患者的事件日志。下面的脚本给出了一个数据框,其中包含事件日志中的跟踪或活动序列,trace_id以及特定跟踪后的案例的绝对频率。我希望使用ggplot2plotly创建一个动态水平条形图,以便跟踪表示为附加的快照,其中带有轴标签的条形顶部的绝对频率为%。

谢谢,请帮助!

library("bupaR")
traces(patients, output_traces = T, output_cases = F)

Trace Chart Explorer

1 个答案:

答案 0 :(得分:0)

希望这有帮助(但我无法获得频率)

library(splitstackshape)

tr <- data.frame(traces(patients, output_traces = T, output_cases = F))
tr.df <- cSplit(tr, "trace", ",")

tr.df <- tr.df[,c(1,4:9)]
tr.df <- melt(tr.df, id.vars = "trace_id")

windows()
ggplot(data = tr.df, aes(x = variable,y = trace_id, fill = value, label = 
value)) + 
geom_tile(colour = "white") + 
geom_text(colour = "white", fontface = "bold", size = 2) +
scale_fill_discrete(na.value="transparent") +
theme(legend.position="none")

enter image description here

编辑1:

library(splitstackshape)
library(bupaR)
library(ggplot2)

tr <- data.frame(traces(patients, output_traces = T, output_cases = F))
tr.df <- cSplit(tr, "trace", ",")

tr.df <- tr.df[,c(1,4:9)]
tr.df <- melt(tr.df, id.vars = "trace_id")
tr.df <- tr.df[order(tr.df$trace_id),]

tr.label <- data.frame(id = tr$trace_id, freq = tr$absolute_frequency)
tr.label <- tr.label[order(tr.label$id),]

windows()
ggplot(data = tr.df, aes(x = variable,y = trace_id, fill = value, label = value)) + 
geom_tile(colour = "white") + 
geom_text(colour = "white", fontface = "bold", size = 2) +
scale_fill_discrete(na.value="transparent") +
theme(legend.position="none") + 
geom_text(data = tr.label, aes(label = freq, y = id, x = 6), nudge_x = 0.3,  
          colour = "black", fontface = "bold", inherit.aes = FALSE) 

enter image description here