我有一些带有id
, time_to_event
, event
, 治疗start
和治疗stop
变量的数据。这是一个可重现的示例:
data <- structure(list(id = structure(c(1L, 3L, 9L, 2L, 5L, 10L, 7L,
8L, 4L, 6L), .Label = c("1", "4", "2", "9", "5", "10", "7", "8",
"3", "6"), class = "factor"), event = c("Death", "Death", "Death",
"Y", "Death", "Y", "Y", "Y", "Death", "X"), time_to_event = c(89L,
83L, 74L, 88L, 78L, 72L, 77L, 76L, 79L, 78L), start = c(18L,
3L, 13L, 16L, 10L, 6L, 20L, 11L, 14L, 9L), stop = c(89L, 83L,
74L, 88L, 78L, 72L, 77L, 76L, 79L, 78L)), row.names = c(NA, -10L
), class = "data.frame")
我计算了一个图,它显示了 as 的类型event
和治疗geom_point
的时间 asgeom_segment
和。这是代码:start
stop
colors <- c("X" = "dodgerblue3",
"Y" = "red2",
"Death" = "black") # Defining the colors for the event type
event_symbols <- c("X" = "\u25CF",
"Y" = "\u25CF",
"Death" = "\u2020") # <-- Problem: When I use the shape 84 (T) the code is plotted. When I use "/u2020", I get the above-mentioned error message
five_day_lines <- seq(0, 90, 5)
day_lines <- 1:90
day_lines <- day_lines[!day_lines %in% five_day_lines]
data$id <- factor(data$id, levels = data$id[order(data$time_to_event, decreasing = T)])
ggplot(data = data) +
geom_hline(yintercept = five_day_lines, color = "grey", alpha = .35) +
geom_hline(yintercept = day_lines, color = "grey", alpha = .25) +
geom_segment(aes(x = id, xend = id, y = start, yend = stop), color = "dimgray", size = 1) +
geom_point(aes(x = id, y = time_to_event, shape = event, color = event), size = 3) +
scale_y_continuous(limits = c(0,90), breaks = c(seq(0, 90, 5)), name = "Days after study inclusion") +
scale_x_discrete(name = "ID") +
coord_flip() +
scale_color_manual(values = colors, breaks = c()) +
scale_shape_manual(values = event_symbols, breaks = c("X", "Y", "Death"),
guide = guide_legend(override.aes = list(color = c("dodgerblue3", "red2", "black")))) +
theme(legend.position = "bottom",
legend.title = element_text(size=12, face="bold"),
panel.background = element_blank(),
legend.background = element_blank()) +
labs(color="Medication", shape="Event type")
情节如下所示:
我的问题是,是否可以geom_segment
在某个点添加一个间隙,比如说在段长度的一半处。理想情况下,中断应该看起来像绘图轴中的“典型”间隙,如下所示https://rstudio-pubs-static.s3.amazonaws.com/235467_5abd31ab564a43c9ae0f18cdd07eebe7.html:
在这里,我们可以看到一种双斜线。我有使用双斜杠 unicode 字符(Link)的想法,但我认为这只会生成一个新层,并且会忽略geom_segment
. 有没有一种聪明的方法来计算这样的差距,在geom_segment
一个斜线处结束并在第二个斜线处重新开始?理想情况下,它适用于双斜线,但如果有另一种合适的形状或其他方式,我会很好。
如果有解决此问题的智能解决方案,我将不胜感激。非常感谢。