Creating a horizontal bar chart in R to display sequence of activities

六月ゝ 毕业季﹏ 提交于 2019-12-02 11:09:21

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")

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) 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!