简体   繁体   English

用ggplot绘制重叠的垂直线

[英]Plot overlapping vertical lines with ggplot

I have a list of time-ordered pairwise interactions. 我有一个按时间顺序的成对交互的列表。 I want to plot a temporal network of these interactions, which would look something like the diagram below. 我想绘制这些交互作用的时间网络,看起来像下面的图。

时态网络示例图像

My data looks like the example below. 我的数据如下例所示。 The id1 and id2 values are the unique identifiers of individuals. id1和id2值是个人的唯一标识符。 The time indicates when an interaction betweens those individuals occurred. 时间指示这些人之间何时发生互动。 So at time = 1, I want to plot a connection between individual-1 and individual-2. 所以在时间= 1时,我想绘制个人1和个人2之间的联系。

id1 <- c(1, 2, 1, 6, 2, 2, 1)
id2 <- c(2, 4, 5, 7, 3, 4, 5)
time <- c(1, 2, 2, 2, 3, 4, 5)
df <- data.frame(id1, id2, time)

According to this StackOverflow question , I can see that it is possible to draw vertical lines between positions on the y-axis in ggplot. 根据这个StackOverflow问题 ,我可以看到有可能在ggplot中的y轴位置之间绘制垂直线。 This is achieved by reshaping the data into a long format. 这可以通过将数据重新整形为长格式来实现。 This is fine when there is only one pair per time value, but not when there is more than one interacting pair at a time. 当每个时间值只有一对时,这很好,但是一次有多个交互对时,则不行。 For example in my dummy data, at time = 2, there are three pairs (in the plot I would show these by overlaying lines with reduced opacity). 例如,在我的虚拟数据中,在时间= 2时,有三对(在图中,我将通过覆盖不透明度降低的线来显示它们)。

My question is, how can I organise these data in a way that ggplot will be able to plot potentially multiple interacting pairs at specified time points? 我的问题是,如何以ggplot能够在指定时间点绘制潜在多个交互对的方式来组织这些数据?

I have been trying to reorganise the data by assigning an extra identifier to each of the multiple pairs that occur at the same time. 我一直在尝试通过为同时出现的多个对中的每一个分配一个额外的标识符来重组数据。 I imagined the data table to look like this , but I haven't figure out how to make this in R... In this example the three interactions at time = 2 are identified by an extra grouping of either 1, 2 or 3. Even if I could arrange this I'm still not sure how I would get ggplot to read it. 我以为数据表看起来像这样 ,但是我还没有弄清楚如何在R中创建这个数据表。在这个示例中,时间= 2的三个交互通过额外的1、2或3分组来标识。即使我可以安排它,我仍然不确定如何使ggplot读取它。

Ultimately I'm trying to create someting that looks like Fig. 2 in this scientific paper . 最终,我试图在本科学论文中创建类似于图2的东西。

Any help would be appreciated! 任何帮助,将不胜感激!

You can do this without reshaping the data, just set one id to y and the other id to yend in geom_curve : 您可以执行此操作而无需重塑数据,只需在geom_curve中将一个id设置为y ,将另一个idyend geom_curve

ggplot(df, aes(x = time, y = id1)) +
    geom_curve(aes(xend = time, yend = id2), curvature = 0.3) +
    geom_hline(yintercept = 1:7, colour = scales::muted("blue")) +
    geom_point(size = 3) +
    geom_point(aes(y = id2), size = 3) +
    coord_cartesian(xlim = c(0, max(df$time) + 1)) +
    theme_bw()

Output: 输出:

在此处输入图片说明

Libraries: 图书馆:

library('ggplot2')
library('data.table')

Data: 数据:

id1 <- c(1, 2, 1, 6, 2, 2, 1)
id2 <- c(2, 4, 5, 7, 3, 4, 5)
time <- c(1, 2, 2, 2, 3, 4, 5)
df <- data.frame(id1, id2, time)
setDT(df)
df1 <- melt.data.table( df, id.vars = c('time'))

Plot: 情节:

p <- ggplot( df1, aes(time, value)) + 
  geom_point() + 
  geom_curve( mapping = aes(x = time, y = id1, xend = time, yend = id2, colour = "curve"), 
              data = df, 
              curvature = 0.2 )
print(p)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM