fct_reorder by date on recurrent items

myzjeezk  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(82)

试图为我们有时不得不给予病人的复杂抗生素治疗方案制作甘特。
有时抗生素剂量改变,然后放回原始剂量,或抗生素完全改变,然后在以后再次改变。
尝试在下面的reprex中捕获问题。

library(tidyverse)
library(lubridate)

df <- tribble(
  ~id,  ~start, ~end, ~antibiotic,
  1,    "02/02/23", "22/02/23", "A 1g",
  2,    "22/02/23", "10/03/23", "A 2g",
  3,    "10/03/23", "15/03/23", "A 1g",
  4,    "28/02/23", "11/03/23", "B 1g",
  5,    "11/03/23", "03/04/23", "B 2g",
  6,    "03/04/23", "10/04/23", "B 1g")

# trying to reorder the antibiotic factor level based on start date then pivot longer into tidy data form
df <- df%>%
  mutate(across(c("start", "end"), dmy),
         antibiotic = fct_reorder(antibiotic, start, .desc = TRUE)) %>%
  pivot_longer(
    cols = c("start", "end"),
    names_to = "start_end",
    values_to = "date"
  )

# create gantt style plot
df %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = antibiotic, colour = antibiotic, group = id), linewidth = 5)

我希望抗生素B1 g显示在B2 g上面,因为它的开始日期在B2 g之前,就像抗生素A一样。
当我检查因子水平时,抗生素A的顺序似乎是正确的,但抗生素B的顺序却颠倒了:

# levels should be "A 1g" "A 2g"  "B 1g"  "B 2g"

rev(levels(df$antibiotic))
#> [1] "A 1g" "A 2g" "B 2g" "B 1g"

我不明白为什么fct_reorder对A正确地做了这件事,而对B却不正确。
如果有人能解释一下,我会很感激的。
创建于2023-04-03带有reprex v2.0.2

e5njpo68

e5njpo681#

这是因为因子的每个水平都有多个起始日期。fct_reorder()不知道要选择哪一个。一个选项是使用slice_min()并根据显示的第一个日期构造因子水平:

antibiotic_levels <- df |> 
  mutate(across(c("start", "end"), dmy)) |> 
  group_by(antibiotic) |> 
  slice_min(start) |> 
  arrange(start) |> 
  mutate(antibiotic = factor(antibiotic, levels = antibiotic)) |> 
  pull(antibiotic)

df %>%
  mutate(across(c("start", "end"), dmy),
         antibiotic = factor(antibiotic, levels = rev(antibiotic_levels))) |> 
  pivot_longer(
    cols = c("start", "end"),
    names_to = "start_end",
    values_to = "date"
  ) %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = antibiotic, colour = antibiotic, group = id), linewidth = 5)

其给出:

相关问题