我有伯纳德数据集的基本图,它显示了治疗的死亡率状态。
library(tidyverse)
library(pubh)
Bernard %>% select(fate, treat, followup) %>%
ggplot(aes(x = treat, fill = fate)) +
geom_bar(stat = "count")
我想要一个动画图,添加向后运行的变量后续(1-720 小时)并显示计数受到的影响。
当然,这只会影响死亡人数(即减少人数),我仍然对这个概念感兴趣,而不是对产出感兴趣。
我试过了
transition_reveal()
:
libary(gganimate)
Bernard %>% select(fate, treat, followup) %>%
ggplot(aes(x = treat, fill = fate)) +
geom_bar(stat = "count") +
transition_reveal(-followup) +
labs(title = "Follow-up time: {-frame_along}"
我建议使用一些预处理将“流量”数据点(记录的死亡人数)转换为“库存”(当前活着的参与者的数量)。可能有一种更简洁的方法来解决这个问题,但我希望清楚发生了什么:
library(tidyverse)
Bernard %>%
count(treat, fate, followup) %>%
mutate(status = n * if_else(fate == "Alive", 1, -1)) %>%
group_by(treat) %>%
arrange(-followup) %>%
mutate(alive_count = cumsum(status),
dead_count = max(n) - alive_count) %>%
ungroup() %>%
select(treat, followup, alive_count, dead_count) %>%
complete(treat, followup) %>%
fill(ends_with("count")) %>%
pivot_longer(alive_count:dead_count) %>%
ggplot(aes(treat, value, fill = name)) +
geom_col() +
transition_manual(-followup)