R脚本。我正在尝试创建以下虚拟数据的患者配置文件图。
id = c(51308),
med_name = c("morphine", "codeine", "diamorphine", "codeine", "morphine", "codeine"),
p_start = c("2010-04-29 12:31:58"),
p_end = c("2011-05-19T14:05:00Z"),
mid_point_dates = c("2010-05-09T14:05:00Z", "2010-04-29T14:05:00Z", "2010-05-01T12:52:14Z", "2010-05-13T14:04:00Z", "2010-05-03T14:04:00Z", "2010-04-30T10:34:27Z")
id == y轴,date == x轴,p_start、mid_point_dates和p_end dates ==数据点。
我做了多次尝试都没有成功。
使用虚拟数据,我已经完成了以下操作,但是,我想应用于多个ID。
# Load required libraries
library(ggplot2)
library(lubridate)
# Define the data
id = c(51308)
med_name = c("morphine", "codeine", "diamorphine", "codeine", "morphine", "codeine")
p_start = ymd_hms("2010-04-29 12:31:58")
p_end = ymd_hms("2010-05-19T14:05:00Z")
mid_point_dates = ymd_hms(c("2010-05-09T14:05:00Z", "2010-04-29T14:05:00Z", "2010-05-01T12:52:14Z", "2010-05-13T14:04:00Z", "2010-05-03T14:04:00Z", "2010-04-30T10:34:27Z"))
# Create a data frame
df <- data.frame(id, med_name, p_start, p_end, mid_point_dates)
# Define the y-axis categories
id_categories <- unique(df$id)
# Create the plot
ggplot(df, aes(x = mid_point_dates, y = id)) +
geom_segment(aes(xend = p_end, yend = id, colour = med_name), size = 2, color = "grey") +
geom_point(aes(colour = med_name, group = med_name, shape = med_name), size = 3) +
scale_color_manual(values = c("purple", "green", "orange")) +
scale_shape_manual(values = c(17, 15, 16)) +
scale_y_discrete(limits = id_categories) +
labs(title = "Medication Timeline", x = "Date", y = "Patient ID") +
theme_minimal()
并生成了以下图表
更新。我调整了r脚本,以允许多个id和日期时间的差异,如下所示。(p_start,p_end和数据点之间的重叠导致前者被带到前面,这不是故意的)
# Load required libraries
library(ggplot2)
library(lubridate)
# Define the data
id = c(214441, 214441, 214441, 214444, 214444, 214446)
med_name = c("morphine", "codeine", "diamorphine", "codeine", "morphine", "drug_X")
p_start = ymd_hms("2010-04-29 12:31:58", "2010-04-29 12:31:58", "2010-04-29 12:31:58", "2010-04-25 12:31:58", "2010-04-25 12:31:58", "2010-04-25 12:31:58")
p_end = ymd_hms("2010-05-19T14:05:00Z", "2010-05-19T14:05:00Z", "2010-05-19 12:31:58", "2010-05-27 12:31:58", "2010-05-27 12:31:58", "2010-05-20 12:31:58")
mid_point_dates = ymd_hms(c("2010-05-09T14:05:00Z", "2010-04-29T14:05:00Z", "2010-05-01T12:52:14Z", "2010-05-13T14:04:00Z", "2010-05-03T14:04:00Z", "2010-04-30T10:34:27Z"))
# Create a data frame
df <- data.frame(id, med_name, p_start, p_end, mid_point_dates)
# Define the y-axis categories
id_categories <- unique(df$id)
ggplot(df, aes(x = mid_point_dates, y = id)) +
geom_segment(aes(x = p_start, xend = p_end, yend = id, colour = med_name), size = 2, color = "grey") +
geom_point(aes(x = mid_point_dates, y = id, colour = med_name, group = med_name), size = 3) +
geom_point(aes(x = p_start, y = id, fill = "white"), size = 3, shape = 23, color = "black") +
geom_point(aes(x = p_end, y = id), size = 3, shape = 23, color = "black") +
scale_color_manual(values = c("purple", "green", "orange", "blue"), name = "medication") +
scale_shape_manual(values = c(17, 15, 16, 2), name = "Start/End") +
scale_fill_manual(values = c("white"), name = "p_start/p_end") +
scale_y_discrete(limits = id_categories) +
labs(title = "Medication Timeline", x = "Date", y = "Patient ID") +
theme_minimal()
1条答案
按热度按时间9vw9lbht1#
为多个患者创建一个数据对象(一个列表,组合成一个tibble),你可以这样做:
这给了你一个这样的情节(它是粗糙和肮脏的,但你得到的想法):