英文:
fct_reorder by date on recurring items
问题
尝试创建复杂抗生素疗程的甘特样式图表,有时需要给患者。有时抗生素剂量会更改,然后恢复到原始剂量,或者在稍后日期之前完全更改抗生素,然后再次更改。
试图在下面的 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")
# 尝试根据开始日期重新排列抗生素因子水平,然后将其纵向展开为整洁数据形式
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"
)
# 创建甘特样式图
df %>%
ggplot(aes(x = date)) +
geom_line(aes(y = antibiotic, colour = antibiotic, group = id), linewidth = 5)
我期望抗生素 B 1g 在抗生素 B 2g 之上显示,因为它的开始日期在 B 2g 之前,就像抗生素 A 一样。
当我检查因子水平时,抗生素 A 似乎是正确的顺序,但抗生素 B 已经交换了顺序:
# 水平应为 "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 不能。如果有人能解释这个问题,将不胜感激。
英文:
Trying to make Gantt style chart for complex antibiotic regimens we sometimes have to give patients.
Sometimes the antibiotic dose is changed then put back to original dose, or antibiotic changed completely before then being changed back again at later date.
Tried to capture problem in reprex below.
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)
<!-- -->
I was expecting antibiotic B 1g to be diplayed above B 2g, as it has a start date before B 2g, just like antibiotic A.
When I check the factor levels, antibiotic A appears to be in correct order but antibiotic B has swapped around:
# levels should be "A 1g" "A 2g" "B 1g" "B 2g"
rev(levels(df$antibiotic))
#> [1] "A 1g" "A 2g" "B 2g" "B 1g"
I can't get my head round why fct_reorder
is doing this correctly for A but not B.
Would be grateful if anyone could explain this.
<sup>Created on 2023-04-03 with reprex v2.0.2</sup>
答案1
得分: 0
以下是翻译好的代码部分:
这是因为你对于因子的每个级别都有多个开始日期。`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)
希望这对你有帮助。
英文:
It's because you have multiple start dates for each level of the factor. fct_reorder()
doesn't know which one to choose. One option is to use slice_min()
and construct the factor levels based on the first date shown:
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)
which gives:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论