Creating a dynamic chart displaying sequences of activities with their count in R

假如想象 提交于 2019-12-02 17:04:09

问题


If you run the R script below, the dataset "patients" is an eventlog of patients visiting a clinic and getting treatment. The trace explorer gets created as in the snapshot below with the tooltip displayed. Now in the "#Script for Frequency Percentage", you get the frequency percentage for each trace in the column "af_percent". My requirement is that, I just want to replace the "label = value" in the ggplot command below with corresponding frequency percentage of each trace. Please help.

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


回答1:


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



来源:https://stackoverflow.com/questions/47261388/creating-a-dynamic-chart-displaying-sequences-of-activities-with-their-count-in

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