【问题标题】:ggplot2 - how to create a clustered timeline?ggplot2 - 如何创建集群时间线?
【发布时间】:2017-07-21 19:29:04
【问题描述】:

您将如何在 R 中创建下面的图表?我想为不同的患者展示不同治疗的持续时间。

在这里模拟数据:

                       Start Day    Stop Day
Patient 1   Drug 1     1            3
            Drug 2     2            5
            Drug 3     3            8
Patient 2   Drug 1     2            4
            Drug 2     2            5
            Drug 3     1            6
Patient 3   Drug 1     4            7
            Drug 2     3            8
            Drug 3     5            6

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的图表可以使用ggplot2 包中的geom_segment 生成:

    df <- structure(list(Patient = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
    3L, 3L, 3L), .Label = c("Patient1", "Patient2", "Patient3"), class = "factor"), 
        Drug = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Drug1", 
        "Drug2", "Drug3"), class = "factor"), StartDay = c(1L, 2L, 
        3L, 2L, 2L, 1L, 4L, 3L, 5L), StopDay = c(3L, 5L, 8L, 4L, 
        5L, 6L, 7L, 8L, 6L)), .Names = c("Patient", "Drug", "StartDay", 
    "StopDay"), class = "data.frame", row.names = c(NA, -9L))
    
    df$Drug <- factor(df$Drug, levels(df$Drug)[c(3,2,1)])
    
    library(ggplot2)
    ggplot(data=df, aes(color=Drug))+
          geom_segment(aes(x=StartDay, xend=StopDay, y=Drug, yend=Drug),lwd=12)+
          facet_grid(Patient~.)+xlab("Days")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多