【问题标题】:Swimmer plot in R (ggplot): How to order stacked bars?R(ggplot)中的游泳者图:如何订购堆叠条?
【发布时间】:2018-12-03 10:52:17
【问题描述】:

我有一个关于使用 R 中的 GGplot 在游泳者图中对堆叠条进行排序的问题。

我有一个接受治疗的(人工)患者的样本数据集。

library(tidyverse)

df <- read.table(text="patient start_t_1 t_1_duration start_t_2 t_2_duration start_t_3 t_3_duration start_t_4 t_4_duration end
                 1    0    1.5    1.5   3   NA    NA    4.5    10   10
                 2    0    2    4.5    2    NA    NA    2   2.5   10
                 3    0    5    5   2   7   0.5   7.5   2   9.5
                 4    0    8    NA    NA    NA    NA    8   2   10", header=TRUE)

所有患者在时间 = 0 开始第一次治疗。随后,患者接受不同的治疗(编号 t_2 到 t_4)。

我尝试使用以下代码绘制游泳者图:

df %>% 
  gather(variable, value, c(t_1_duration, t_2_duration, t_3_duration, t_4_duration)) %>% 
  ggplot(aes(x = patient, y = value, fill = variable)) + 
  geom_bar(stat = "identity") +
  coord_flip()

但是,治疗没有按正确的顺序显示。 例如:患者 3 依次接受所有治疗,而患者 2 先接受治疗 1,然后接受治疗 4,最后接受治疗 2。 所以,简单地颠倒顺序是行不通的。

如何按时间顺序排列堆叠的条形?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这个呢:

    df %>% 
      gather(variable, value, c(t_1_duration, t_2_duration, t_3_duration,t_4_duration)) %>% 
      ggplot(aes(x = patient,
                 y = value,
                 # here you can specify the order of the variable
                 fill = factor(variable, 
                              levels =c("t_4_duration", "t_3_duration", "t_2_duration","t_1_duration")))) + 
      geom_bar(stat = "identity") +
      coord_flip()+ guides(fill=guide_legend("My title")) 
    

    编辑: 这是一个漫长的旅程,因为它涉及kind of hack。我认为这不是对那个问题的欺骗,因为它还涉及一些数据重塑:

    library(reshape2)
    
    # divide starts and duration
    starts <- df %>% select(patient, start_t_1, start_t_2, start_t_3, start_t_4) 
    duration <- df %>% select(patient, t_1_duration,t_2_duration, t_3_duration, t_4_duration)
    
    # here you melt them
    starts <- melt(starts, id = 'patient')  %>%
      mutate(keytreat = substr(variable,nchar(as.vector(variable))-2, nchar(as.vector(variable)))) %>%
      `colnames<-`(c("patient", "variable", "start","keytreat")) %>% select(-variable)
    duration <- melt(duration, id = 'patient')  %>% mutate(keytreat = substr(variable,1, 3)) %>%
      `colnames<-`(c("patient", "variable", "duration","keytreat")) %>% select(-variable)
    
    # join
    dats <- starts %>% left_join(duration) %>% arrange(patient, start) %>% filter(!is.na(start))
    
    
    # here the part for the plot
    bars <- map(unique(dats$patient)
                , ~geom_bar(stat = "identity", position = "stack"
                            , data = dats %>% filter(patient == .x)))
    
    dats %>% 
      ggplot(aes(x = patient,
                 y = duration,
                 fill = reorder(keytreat,-start))) + 
      bars +
      guides(fill=guide_legend("ordering"))  + coord_flip()
    

    【讨论】:

    • 它更接近解决方案,然后我就是我自己,所以谢谢:)!但是,排序尚未按时间顺序排列。例如:患者 2 接受第一次治疗 1,然后接受治疗 4 和最后一次治疗 2,而图表中的颜色对应于治疗 1 -> 2 -> 4。因此治疗顺序基于治疗开始的时间.知道如何合并它吗?
    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多