Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的游泳者图(ggplot):如何排列堆叠的酒吧?_R_Ggplot2 - Fatal编程技术网

R中的游泳者图(ggplot):如何排列堆叠的酒吧?

R中的游泳者图(ggplot):如何排列堆叠的酒吧?,r,ggplot2,R,Ggplot2,我有一个关于使用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 N

我有一个关于使用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)
但是,这些处理没有按正确的顺序显示。 例如:患者3按连续顺序接受所有治疗,而患者2先接受治疗1,然后接受治疗4,最后接受治疗2。 因此,简单地颠倒顺序是行不通的

如何按时间顺序排列堆叠的钢筋?

这是什么:

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

编辑: 这是一个漫长的旅程,因为它涉及到一个复杂的问题。我认为这不是这个问题的翻版,因为它还涉及一些数据重塑:

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()
library(重塑2)
#划分起点和持续时间
开始百分比选择(患者、开始测试1、开始测试2、开始测试3、开始测试4)
持续时间%选择(患者、t_1_持续时间、t_2_持续时间、t_3_持续时间、t_4_持续时间)
#在这里你可以融化它们
开始%
mutate(keytreat=substr(variable,nchar(as.vector(variable))-2,nchar(as.vector(variable)))%>%
`colnames%select(-variable)
持续时间%变异(keytreat=substr(变量,1,3))%>%
`colnames%select(-variable)
#加入
数据%left_加入(持续时间)%%>%arrange(患者,开始)%%>%filter(!is.na(开始))
#这是情节的一部分
条百分比过滤器(患者=.x)))
数据%>%
ggplot(不良事件(x=患者,
y=持续时间,
填充=重新排序(keytreat,-start))+
栅栏+
导轨(填充=导轨图例(“订购”)+coord\u flip()

这更接近解决方案,那时我就是我自己,所以谢谢:)!然而,顺序尚未按时间顺序排列。例如:患者2首先接受治疗1,然后接受治疗4,最后接受治疗2,而图中的颜色对应于治疗1->2->4。因此,治疗顺序是基于治疗开始的时间。你知道如何合并这个吗?
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()