r - 在 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
和。这是代码: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
一个斜线处结束并在第二个斜线处重新开始?理想情况下,它适用于双斜线,但如果有另一种合适的形状或其他方式,我会很好。
如果有解决此问题的智能解决方案,我将不胜感激。非常感谢。
解决方案
我认为 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)
推荐阅读
- sql - 如果行状态改变,则删除行
- c# - 基于非主键的ef核心查询
- kubernetes - 谁启动 kube-apiserver 以及如何配置其启动参数?
- reactjs - 来自另一个 React 类中使用的一个 React 类的状态值
- php - PUT 请求到带有标头的另一台服务器不起作用
- php - 使用 PHP 类方法从按钮单击计数器
- javascript - 如何在点击事件jquery中使用点击事件
- android - Android SQLite 插入比 iOS 慢
- tensorflow - Keras 在 CondInst 中使用 tensorflow 实现动态过滤器结构
- c# - 航点超出范围错误如何解决?