在 ggplot2 中的 geom_segment 中添加间隙

问题描述

我有一些包含 idtime_to_eventevent、疗法 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","Y","X"),time_to_event = c(89L,83L,74L,88L,78L,72L,77L,76L,79L,78L),start = c(18L,13L,16L,6L,20L,11L,14L,9L),stop = c(89L,78L)),row.names = c(NA,-10L
),class = "data.frame")

我计算了一个图,其中 event 的类型为 geom_point,治疗时间为 geom_segmentstartstop代码如下:

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,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,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","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")

情节如下:

Example Plot

我的问题是,是否可以在某个点在 geom_segment添加一个间隙,比如说在段长度的一半处。理想情况下,中断应该看起来像图轴中的“典型”间隙,如下所示 https://rstudio-pubs-static.s3.amazonaws.com/235467_5abd31ab564a43c9ae0f18cdd07eebe7.html

Example

在这里,我们可以看到一种双斜线。我有采用双斜线 unicode 字符 (Link) 的想法,但我认为,这只会生成一个新层,而忽略 geom_segment 所需的间隙或“中断”。有没有一种聪明的方法来计算这样的差距,其中 geom_segment一个斜线处结束并在第二个斜线处再次开始?理想情况下,它适用于双斜线,但如果有另一种具有其他适当形状或其他东西的方式,我会很好。

如果有针对此问题的明智解决方案,我将不胜感激。 非常感谢。

解决方法

我认为 ggplot2 或扩展中不存在专门处理间隙的专门层。人们当然可以滥用 arrow 中的 geom_segment() 参数在段的两端绘制粗短的箭头,但这可能不会减轻使用第二层的必要性。您将在下面找到一个简化示例。

library(ggplot2)

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","Y","X"),time_to_event = c(89L,83L,74L,88L,78L,72L,77L,76L,79L,78L),start = c(18L,13L,16L,6L,20L,11L,14L,9L),stop = c(89L,78L)),row.names = c(NA,-10L),class = "data.frame"
)

gap_start <- 30
gap_stop <- 50

ggplot(data,aes(x = start,xend = stop,y = id,yend = id)) +
  geom_segment(aes(xend = gap_start),arrow = arrow(90,unit(1,"mm"))) +
  geom_segment(aes(x = gap_stop),"mm"),ends = "first"))

reprex package (v1.0.0) 于 2021 年 7 月 14 日创建