我有一些患者数据,其中个别患者会随着时间的推移改变治疗组。我的目标是可视化组更改的顺序,并将这些数据汇总到每个治疗组的“顺序配置文件”中。
对于我想展示的每个治疗组,它通常发生在治疗周期中的时间(比如在开始或结束时)。为了解释不同的序列长度,我想在 0(最开始)和 1(结束)之间标准化这些配置文件。
我想找到一个有效的数据准备和可视化。
最小示例
数据结构
library(dplyr)
library(purrr)
library(ggplot2)
# minimal data
cj_df_raw <- tibble::tribble(
~id, ~group,
1, "A",
1, "B",
2, "A",
2, "B",
2, "A"
)
# compute "intervals" for each person [start, end]
cj_df_raw %>%
group_by(id) %>%
mutate(pos = row_number(),
len = length(id),
start = (pos - 1) / len,
end = pos / len) %>%
filter(group == "A")
#> # A tibble: 3 x 6
#> # Groups: id [2]
#> id group pos len start end
#> <dbl> <chr> <int> <int> <dbl> <dbl>
#> 1 1 A 1 2 0 0.5
#> 2 2 A 1 3 0 0.333
#> 3 2 A 3 3 0.667 1
(因此,Id 1 在其序列的前 50% 中位于 A 组,而 Id 2 在其序列的前 33% 和后 33% 中位于 A 组。这意味着,2 个 Id 在 0-33% 之间序列中,1 在 33-50% 之间,0 在 50-66% 之间,1 在 66% 以上。)
这是我想要达到的结果,但我错过了有效转换数据的机会。
期望的结果
profile_treatmen_a <- tibble::tribble(
~x, ~y,
0, 0L,
0.33, 2L,
0.5, 1L,
0.66, 0L,
1, 1L,
1, 0L
)
profile_treatmen_a %>%
ggplot(aes(x, y)) +
geom_step(direction = "vh") +
expand_limits(x = c(0, 1), y = 0)
(理想情况下,曲线下的区域会被遮蔽)
理想的解决方案:通过 ggridges
可视化的目标是同时比较许多治疗组的“序列概况”。如果我可以相应地准备数据,我想使用 ggridges-package 对治疗组进行惊人的视觉比较。
library(ggridges)
data.frame(group = rep(letters[1:2], each=20),
mean = rep(2, each=20)) %>%
mutate(count = runif(nrow(.))) %>%
ggplot(aes(x=count, y=group, fill=group)) +
geom_ridgeline(stat="binline", binwidth=0.5, scale=0.9)