创建一个动态图表,显示其计数为R的活动序列

时间:2017-11-13 09:50:44

标签: r ggplot2 plotly ggplotly visnetwork

如果您运行下面的R脚本,数据集"患者"是访问诊所并接受治疗的患者的事件日志。跟踪资源管理器将在下面的快照中创建,并显示工具提示。现在,在##; #Script for Frequency Percentage"中,您可以获得列#34; af_percent"中每条曲线的频率百分比。我的要求是,我只想替换" label = value"在下面的ggplot命令中,每个跟踪的频率百分比相应。请帮忙。

library(splitstackshape)
library(scales)
library(ggplot2)
library(plotly)
tr <- data.frame(traces(patients, output_traces = T, output_cases = F))
tr.df <- cSplit(tr, "trace", ",")
tr.df$af_percent <-
percent(tr.df$absolute_frequency/sum(tr.df$absolute_frequency))
pos <- c(1,4:ncol(tr.df))
tr.df <- tr.df[,..pos]
tr.df <- melt(tr.df, id.vars = c("trace_id","af_percent"))
mp1 = 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")
ggplotly(mp1)

#Script for Frequency Percentage
tr = traces(patients, output_traces = T,output_cases = F) 
tr$af_percent = percent(te$absolute_frequency/sum(te$absolute_frequency))

enter image description here

1 个答案:

答案 0 :(得分:2)

library(bupaR)
library(plotly) 

traces(patients, output_traces = T, output_cases = F)

p <- trace_explorer(patients,type = "frequent", coverage = 1) +
theme(axis.text.x=element_blank(),
      axis.ticks.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks.y=element_blank())

gg <- ggplotly(p)

# Now manipulate the plotly object in the same way that 
# we would manipulate any other plotly object
# See https://plotly-book.cpsievert.me/extending-ggplotly.html
layout(gg, margin=list(l=50, b=50), legend=list(x=1.05))

enter image description here