首页 > 解决方案 > 在 ggplot2 中的 geom_segment 中添加一个间隙

问题描述

我有一些带有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和。这是代码:startstop

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一个斜线处结束并在第二个斜线处重新开始?理想情况下,它适用于双斜线,但如果有另一种合适的形状或其他方式,我会很好。

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

标签: rggplot2geom-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", "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"
)

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), arrow = arrow(90, unit(1, "mm"), ends = "first"))

reprex 包于 2021-07-14 创建(v1.0.0)


推荐阅读