【问题标题】:Creating a dynamic chart displaying sequences of activities with their count in R创建一个动态图表,显示活动序列及其在 R 中的计数
【发布时间】:2017-11-13 09:50:44
【问题描述】:

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

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

【问题讨论】:

    标签: r ggplot2 plotly ggplotly visnetwork


    【解决方案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))
    

    【讨论】:

    • @AshminKaul 如果您更改问题,读者会发现我的回答难以理解和令人费解。正确的方法是在第一个问题下方添加包含更多详细信息的文本。
    • 当然,我会确保的,请帮忙。
    • 它是第一个包,赞成您的回答 Marco,请帮助。另外,我想让代码可扩展以适应更大的数据,如果你能帮我删除硬编码,那将是很大的帮助。
    • 添加了 scales 包,第 7 行和第 8 行代码也是硬编码的,请您帮助我使解决方案具有可扩展性和动态性
    • tr.df
    猜你喜欢
    • 2020-02-10
    • 1970-01-01
    • 2021-08-21
    • 2015-11-18
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多