0

The dataset "patients" is an eventlog of patients visiting a clinic and getting treatment. The script below gives a data frame with traces or sequence of activities in the eventlog, trace_id and absolute frequency of the cases following the particular trace. I wish to create a dynamic horizontal bar chart using ggplot2 or plotly such that the traces are represented like the snapshot attached with the absolute frequency in % at the top of the bar with axes labels.

Thanks and please help!

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

Trace Chart Explorer

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37

1 Answers1

0

Hope this helps (I am not able to get frequency however)

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

EDIT 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

Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83
  • very nice, it come out very well, now small help, I want to remove the value labels on the right, and also decrease the text font. Also, if I wish to change the colors, please help me how to do it? Great help. – Ashmin Kaul Nov 14 '17 at 07:54
  • Surely, I'll do that, just that, can you give some light colors to the plot like that snap above, this yellow color seems very bright. – Ashmin Kaul Nov 14 '17 at 08:04
  • Hey Hardik, need some few more tweaks, updated your latest post, please help. – Ashmin Kaul Nov 14 '17 at 08:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158936/discussion-between-hardik-gupta-and-ashmin-kaul). – Hardik Gupta Nov 14 '17 at 08:54
  • please add your edit as a new paragraph below the original, please do not change the original answer – Hardik Gupta Nov 14 '17 at 08:56
  • did as you suggested, please check. – Ashmin Kaul Nov 14 '17 at 09:21