ggplot:显示两条geom_lines之间间隙的显示线

问题描述 投票:0回答:1

我正在尝试模拟此图:enter image description here

但是我不知道如何制作显示标签的线(带有标签),以显示橙色线和紫色线之间的间隙。

r ggplot2
1个回答
0
投票
这是一种方法。我们需要为标签和细分创建一个单独的数据集。然后我们可以使用geom_linegeom_pointgeom_segmentgeom_label

library(tidyverse) # create sample data d <- data.frame(x = rep(1:5, 2), y = c(1:5, 5:9), category = rep(c("a", "b"), each = 5), stringsAsFactors = FALSE) # filter on a specific x value, and reshape the data to be "wide" d_wide <- d %>% filter(x == 5) %>% spread(category, y) ggplot(d, aes(x, y))+ geom_line(aes(colour = category))+ geom_point(aes(colour = category))+ geom_segment(data = d_wide, aes(xend = x, y = a, yend = b))+ geom_label(data = d_wide, aes(label = b - a, y = (b+a) / 2))

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.