我正在学习如何在R中创建圆形图,类似于CIRCOS 我使用包circlize根据航班是OB,入站和返回来绘制始发地和目的地对之间的链接。数据的逻辑并不重要,它只是一个玩具示例
我已经根据下面的代码使用了基于以下逻辑
的代码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航班
所以例如,
任何人都可以提供帮助,有没有人见过以前做过类似的事情? 最终结果应该只是让一个国家出现在地块上,以便有人能够很快看到哪些国家的航班数量最多
我已经尝试过关注other posts,但我担心当他们转向更高级的东西时会迷失方向
非常感谢你的时间
答案 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)
首先,您需要使用 circlize 的开发版本 你可以通过
安装它devtools::install_github("jokergoo/circlize")
在这个新版本中,chordDiagram()
支持输入变量作为数据框并为链接绘制双头箭头(刚看完帖子后):)。
在上面的代码中,col
,directional
,direction.type
和diffHeight
都可以设置为与df
中的行对应的向量。
当directional
中的chordDiagram()
参数设置为2时,相应的链接将有两个方向。然后如果direction.type
包含箭头,则会有一个双头箭头。
由于diffHeight
是一个与df
中的行对应的向量,如果要通过箭头和根的偏移来显示单个链接的方向,则需要合并这两个选项作为单个字符串,如示例代码"arrows+diffHeight"
中所示。
默认情况下,链接的方向是从第一列到第二列。但在您的情况下,IB
表示反向,因此我们需要将diffHeight
设置为负值以反转默认方向。
最后,我发现你有链接在同一扇区(ES-ES-DOM
和US-US-DOM
)开始和结束,你可以使用self.link
参数来控制如何表示这种自我链接。下图中self.link
设置为1。
答案 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(较小边)