gpt4 book ai didi

r - ggplot2 - 如何创建集群时间轴?

转载 作者:行者123 更新时间:2023-12-02 09:17:50 24 4
gpt4 key购买 nike

您将如何在 R 中创建下图?我想展示不同患者不同治疗的持续时间。

Clustered Timeline

此处模拟数据:

                       Start Day    Stop Day
Patient 1 Drug 1 1 3
Drug 2 2 5
Drug 3 3 8
Patient 2 Drug 1 2 4
Drug 2 2 5
Drug 3 1 6
Patient 3 Drug 1 4 7
Drug 2 3 8
Drug 3 5 6

最佳答案

可以使用 ggplot2 包中的 geom_segment 生成您的图表:

df <- structure(list(Patient = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L), .Label = c("Patient1", "Patient2", "Patient3"), class = "factor"),
Drug = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Drug1",
"Drug2", "Drug3"), class = "factor"), StartDay = c(1L, 2L,
3L, 2L, 2L, 1L, 4L, 3L, 5L), StopDay = c(3L, 5L, 8L, 4L,
5L, 6L, 7L, 8L, 6L)), .Names = c("Patient", "Drug", "StartDay",
"StopDay"), class = "data.frame", row.names = c(NA, -9L))

df$Drug <- factor(df$Drug, levels(df$Drug)[c(3,2,1)])

library(ggplot2)
ggplot(data=df, aes(color=Drug))+
geom_segment(aes(x=StartDay, xend=StopDay, y=Drug, yend=Drug),lwd=12)+
facet_grid(Patient~.)+xlab("Days")

enter image description here

关于r - ggplot2 - 如何创建集群时间轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45245368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com