R Circular Chord Plots

时间:2015-08-09 11:12:02

标签: r plot chord-diagram

我正在学习如何在R中创建圆形图,类似于CIRCOS 我使用包circlize根据航班是OB,入站和返回来绘制始发地和目的地对之间的链接。数据的逻辑并不重要,它只是一个玩具示例

我已经根据下面的代码使用了基于以下逻辑

的代码
  1. 获取我的数据,将目标列与航班类型相结合
  2. 转换为矩阵并将原点和新列输入circlize
  3. Reference

    library(dplyr)
    library(circlize)
    
    # Create Fake Flight Information in a table
    orig = c("IE","GB","US","ES","FI","US","IE","IE","GB")
    dest = c("FI","FI","ES","ES","US","US","FI","US","IE")
    direc = c("IB","OB","RETURN","DOM","OB","DOM","IB","RETURN","IB")
    mydf = data.frame(orig, dest, direc)
    
    # Add a column that combines the dest and direction together
    mydf <- mydf %>%
      mutate(key = paste(dest,direc)) %>%
      select (orig, key)
    
    # Create a Binary Matrix Based on mydf
    mymat <- data.matrix(as.data.frame.matrix(table(mydf)))
    
    # create the objects you want to link from to in your diagram
    from <- rownames(mymat)
    to <- colnames(mymat)
    
    # Create Diagram by suppling the matrix 
    par(mar = c(1, 1, 1, 1))
    chordDiagram(mymat, order = sort(union(from, to)), directional = TRUE)
    circos.clear()
    

    我很喜欢这个情节,但想稍微改变一下。例如FI(芬兰)在FI IB,FI OB和FI图上有3个测量值。我想如果可能的话使用一个颜色方案,箭头或甚至增加一个额外的轨道,其充当用于IB OB伞航班的三种类型之间将它们全部结合FI下和区分和RETURN航班

    所以例如,

    • FI OB将被放置在FI中但是具有到GB的单向箭头以表示OB
    • FI IB将被放置在FI中,但是单向箭头进入FI
    • FI RETURN(如果存在)将有一个双头箭头

    任何人都可以提供帮助,有没有人见过以前做过类似的事情? 最终结果应该只是让一个国家出现在地块上,以便有人能够很快看到哪些国家的航班数量最多

    我已经尝试过关注other posts,但我担心当他们转向更高级的东西时会迷失方向

    非常感谢你的时间

2 个答案:

答案 0 :(得分:7)

首先,我认为您的数据中存在重复的记录(IE-FI-IB)。

我将首先附上代码和图,然后再解释一下。

df = data.frame(orig, dest, direc, stringsAsFactors = FALSE)
df = unique(df)
col = c("IB" = "red",
        "OB" = "blue",
        "RETURN" = "orange",
        "DOM" = "green")
directional = c("IB" = -1,
                "OB" = 1,
                "RETURN" = 2,
                "DOM" = 0)
diffHeight = c("IB" = -0.04,
                "OB" = 0.04,
                "RETURN" = 0,
                "DOM" = 0)
chordDiagram(df[1:2], col = col[df[[3]]], directional = directional[df[[3]]], 
    direction.type = c("arrows+diffHeight"),
    diffHeight = diffHeight[df[[3]]])

legend("bottomleft", pch = 15, legend = names(col), col = col)

enter image description here

首先,您需要使用 circlize 的开发版本 你可以通过

安装它
devtools::install_github("jokergoo/circlize")

在这个新版本中,chordDiagram()支持输入变量作为数据框并为链接绘制双头箭头(刚看完帖子后):)。

在上面的代码中,coldirectionaldirection.typediffHeight都可以设置为与df中的行对应的向量。

directional中的chordDiagram()参数设置为2时,相应的链接将有两个方向。然后如果direction.type包含箭头,则会有一个双头箭头。

由于diffHeight是一个与df中的行对应的向量,如果要通过箭头和根的偏移来显示单个链接的方向,则需要合并这两个选项作为单个字符串,如示例代码"arrows+diffHeight"中所示。

默认情况下,链接的方向是从第一列到第二列。但在您的情况下,IB表示反向,因此我们需要将diffHeight设置为负值以反转默认方向。

最后,我发现你有链接在同一扇区(ES-ES-DOMUS-US-DOM)开始和结束,你可以使用self.link参数来控制如何表示这种自我链接。下图中self.link设置为1。

enter image description here

答案 1 :(得分:1)

你需要箭头吗,因为图中的颜色编码已经告诉了From / To故事(FROM - &gt;颜色边缘FROM COUNTRY,TO是来自TO COUNTRY的FROM COUNTRY的颜色,IF FROM ==它自己的颜色返回它自己的基础(例如参见美国或ES))。

library(dplyr)
library(circlize)

# Create Fake Flight Information in a table
orig = c("IE","GB","US","ES","FI","US","IE","IE","GB")
dest = c("FI","FI","ES","ES","US","US","FI","US","IE")
mydf = data.frame(orig, dest)

# Create a Binary Matrix Based on mydf
mymat <- data.matrix(as.data.frame.matrix(table(mydf)))

# create the objects you want to link from to in your diagram
from <- rownames(mymat)
to <- colnames(mymat)

# Create Diagram by suppling the matrix 
par(mar = c(1, 1, 1, 1))
chordDiagram(mymat, order = sort(union(from, to)), directional = TRUE)
circos.clear()

顺便说一下 - &gt;边缘上还有一个OFFSET差异,它告诉它是FROM(较宽边)还是TO(较小边)