在R中创建水平条形图以显示活动序列 [英] Creating a horizontal bar chart in R to display sequence of activities

查看:190
本文介绍了在R中创建水平条形图以显示活动序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



感谢您的帮助!

  library(bupaR) 
trace(patient,output_traces = T,output_cases = F)

解决方案

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

pre $ library(splitstackshape)
$ b $ tr< - data.frame(traces(patient,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(color =white)+
geom_text(color =white,fontface =粗体,size = 2)+
scale_fill_discrete(na.value =透明)+
主题(legend.pos ition =none)



编辑1:
$ b

  library(splitstackshape) 
library(bupaR)
library(ggplot2)
$ b $ tr < - data.frame(traces(patient,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(color =white )+
geom_text(color =white,fontface =bold,size = 2)+
scale_fill_discrete(na.value =transpare nt)+
主题(legend.position =none)+
geom_text(data = tr.label,aes(label = freq,y = id,x = 6),nudge_x = 0.3,
color =black,fontface =bold,inherit.aes = FALSE)


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)

解决方案

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) 

这篇关于在R中创建水平条形图以显示活动序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆