r - ggplot : display line that shows gap between two geom_lines

标签 r ggplot2

我试图通过https://twitter.com/ariamsita来模拟这个图表: enter image description here

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

最佳答案

这里有一个方法。我们需要为标签和分割创建一个与您自己的数据集不同的数据集。然后我们可以使用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

关于r - ggplot : display line that shows gap between two geom_lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61087469/

相关文章:

r - ggplot中的混合线和散点图

r - Linux 环境中默认的 .RData 工作空间保存在哪里?

r - Shiny 的 SelectInput 为 renderImage 生成文件路径

r - 混合效应逻辑回归 : different results with MASS and lme4

r - 定义因子时发出警告:不赞成重复使用因子水平

R - 在 jupyter 中更改 ggplot 绘图大小

R:如何使用 R 代码卡住导出的 Excel 文件的第一行?

arrays - R 中的循环数组

r - 生存曲线中多组的不同颜色类型和线型

r - 如何使用引导值注释 ggtree 对象?