在R中的分段行内的组之间连接开始和结束?

时间:2018-06-14 11:00:47

标签: r ggplot2

我的数据框如下所示:

> df
  person step start end
1    sam    A     0   4
2    sam    B     4   6
3   greg    A     2   7
4   greg    B     7  11

我绘制了下面的图表:

library(ggplot2)
library(dplyr)
library(tidyr)

ggplot(df, aes(colour=step)) + 
  geom_segment(aes(x=start, xend=end, y=person, yend=person), size=3) +
  xlab("Duration")

现在,我想在sam和greg之间连接每个开始和结束,并用差异标记它。

预期的图形看起来像这样:

enter image description here

问题是我不知道如何在不传递x和y坐标的情况下绘制线条。 y坐标由分组(人)确定。

2 个答案:

答案 0 :(得分:1)

不是一个完整的答案,但这可以帮助你实现目标:

您的数据:

df<-
fread("person step start end
sam    A     0   4
sam    B     4   6
greg    A     2   7
greg    B     7  11")

您的主要任务是查找/计算数据1:

data1 <- data.frame(x=2:4,xend=1:3,y=rep(1,3),yend=rep(2,3),textx=1:3+.5,texty=rep(1.5,3),textlabel=letters[1:3])

添加data1:

的信息
ggplot(df, aes(colour=step)) + 
    geom_segment(aes(x=start, xend=end, y=person, yend=person), size=3) +
    xlab("Duration") +
    geom_segment(data = data1,aes(x = x, y = y, xend = xend, yend = yend),colour="black",size=3) +
    geom_text(data = data1,aes(x = textx+0.5, y = texty, label=textlabel),colour="green",size=7)

情节: enter image description here

请注意:

samgreg存储为因子,因此存储来自1 to n的整数值。

  1. 这意味着greg为1,sam为2.(默认情况下,因子按字母顺序排序)

  2. 这意味着greg和sam之间的y位置是1.5

答案 1 :(得分:1)

我按照你的方式编写了以下代码。关键是你需要转换数据,以便绘制线条和标签。对于这些行,我使用了geom_segment()。该函数需要x,xend,y和yend。所以我生成了这些值。对于标签,我使用geom_text(),我需要生成x,y和标签。

library(tidyverse)

# This data transformation is for drawing lines between Greg and Sam

df2 <- gather(df, key = whatever, value = value, -person, -step) %>%
       group_by(person) %>%
       distinct(value) %>%
       arrange(person, value) %>%
       mutate(group = 1:n()) %>%
       spread(key = person, value = value) %>%
       mutate(y = 2, yend = 1)

# This data transformation is for labels

df3 <- mutate(df2, x = (greg + sam) / 2 + 0.4, y = 1.5,
              label = greg - sam)

ggplot(df, aes(colour = step)) + 
geom_segment(aes(x = start, xend = end, y = person, yend = person), size = 3) +
geom_segment(data = df2, aes(x = sam, xend = greg, y = y, yend = yend),
             size = 2, lineend = "round", inherit.aes = F) +
geom_text(data = df3, aes(x = x, y = y, label = label), inherit.aes = F) +
xlab("Duration") 

enter image description here

数据

df <- structure(list(person = c("sam", "sam", "greg", "greg"), step = c("A", 
"B", "A", "B"), start = c(0L, 4L, 2L, 7L), end = c(4L, 6L, 7L, 
11L)), .Names = c("person", "step", "start", "end"), class = "data.frame", row.names = c("1", 
"2", "3", "4"))