简体   繁体   English

Circlize migration plot缺少链接

[英]Circlize migration plot missing links

I have tried to follow the R code from Guy, to create circular migration plots. 我试图遵循Guy的R代码,创建循环迁移图。 My files are similar to those that he presents with the package "migest" (science/regions_custom). 我的文件类似于他用“migest”包(science / regions_custom)提供的文件。 My data file example is 'regions_oceanos': 我的数据文件示例是'regions_oceanos':

-   -   -   1   2   4   3
-   -   -   Pacífico    Índico  Mediterráneo    Atlántico
1   160,0,125   Pacífico    245.025 1.70210026  0.09857195  1.7337982
2   0,255,233   Índico  0.4165425   242.575 3.9052666   6.9741618
4   125,175,0   Mediterráneo    3.05222742  5.99567776  11.125  5.7953056
3   255,219,0   Atlántico   2.63735109  7.51662301  4.3093533   36.625

And the R code is: 而R代码是:

library("migest")
m<-read.table("region_reciente.txt", skip=2, stringsAsFactors=FALSE)
df1<-m[,1:3]
names(df1)<-c("order","rgb","region")
df1$region<-gsub("_", " ", df1$region)
m<-m[,-(1:3)]/1e06
m<-as.matrix(m)
dimnames(m)<-list(orig=df1$region,dest=df1$region)
library("plyr")
df1<-arrange(df1, order)
df1$region <- factor(df1$region, levels=df1$region)  
m<-m[levels(df1$region),levels(df1$region)]
df1$xmin <- 0
df1$xmax <- rowSums(m)+colSums(m)
n<-nrow(df1)
df1 <- cbind(df1, matrix(as.numeric(unlist(strsplit(df1$rgb,","))),nrow=n,   byrow=TRUE) )
names(df1)[ncol(df1)-2:0]<-c("r","g","b")
df1$rcol<-rgb(df1$r, df1$g, df1$b, max = 255)
df1$lcol<-rgb(df1$r, df1$g, df1$b, alpha=200, max = 255)
library("circlize")
par(mar=rep(0,4))
circos.clear()
circos.par(cell.padding=c(0,0,0,0), track.margin=c(0,0.1), start.degree = 90, gap.degree =4)
circos.initialize(factors = df1$region, xlim = cbind(df1$xmin, df1$xmax))
circos.trackPlotRegion(ylim = c(0, 1), factors = df1$region,     track.height=0.1, bg.border = NA, bg.col = NA, bg.lty =0, bg.lwd=0.0001,
panel.fun = function(x, y) {
    name = get.cell.meta.data("sector.index")
    i = get.cell.meta.data("sector.numeric.index")
    xlim = get.cell.meta.data("xlim")
    ylim = get.cell.meta.data("ylim")
    circos.text(x=mean(xlim), y=2.2, labels=name, facing = "bending", cex=0.8)
    circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2], ytop=ylim[2], 
             col = df1$rcol[i], border=df1$rcol[i])
    circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2]-rowSums(m)[i], ytop=ylim[1]+0.3, 
             col = "white", border = "white")
    circos.rect(xleft=xlim[1], ybottom=0.3, xright=xlim[2], ytop=0.32, col = "white", border = "white")
    circos.axis(labels.cex=0.8, direction = "outside", major.at=seq(0,floor(df1$xmax)[i]), minor.ticks=1,
             labels.away.percentage = 0.15)
})
df1$sum1 <- colSums(m)
df1$sum2 <- numeric(n)
df2<-cbind(as.data.frame(m),orig=rownames(m),  stringsAsFactors=FALSE)
df2<-reshape(df2, idvar="orig", varying=list(1:n), direction="long",      timevar="dest", time=rownames(m),  v.names = "m")
df2<-arrange(df2,desc(m))
df2<-subset(df2, m>quantile(m,0.65))
for(k in 1:nrow(df2)){
    i<-match(df2$orig[k],df1$region)
    j<-match(df2$dest[k],df1$region)
    circos.link(sector.index1=df1$region[i], point1=c(df1$sum1[i], df1$sum1[i] + abs(m[i, j])),
          sector.index2=df1$region[j], point2=c(df1$sum2[j], df1$sum2[j] + abs(m[i, j])),
          col = df1$lcol[i])
    df1$sum1[i] = df1$sum1[i] + abs(m[i, j])
    df1$sum2[j] = df1$sum2[j] + abs(m[i, j])
}

And, as you can see I obtain this very cool graph , but the links within it not neccesarily represent my matrix. 并且,正如您所看到的,我获得了这个非常酷的图表 ,但其中的链接并不一定代表我的矩阵。 As an example, you can see that there is not flow from or to Pacifico and Mediterraneo or from Atlantico to Mediterraneo, but in the matrix I present some values for those segments that are similar to those that were actually plotted. 例如,您可以看到没有从Pacifico和Mediterraneo或从Atlantico到Mediterraneo的流量,但在矩阵中,我为那些与实际绘制的段相似的段提供了一些值。 It might be related to the Note that the code presents after creating the plot region: (Note: 1 point is out of plotting region in sector 'Pacífico', track '1'). 它可能与注意代码在创建绘图区域后显示:(注意:1点不在扇区'Pacífico'中的绘图区域,轨道'1')。 But I am not sure how to deal with it. 但我不知道如何处理它。 I will really appreciate any feedback and comments 我将非常感谢任何反馈和意见

The line 这条线

df2 <- subset(df2, m>quantile(m,0.65))

ensures only the biggest flows are plotted. 确保仅绘制最大流量。 You probably don't need this, given you have only four regions, and it sounds like you want to see all the flows. 你可能不需要这个,因为你只有四个区域,听起来你想看到所有的流程。

Also, I have yet to update the CRAN version of the demo script in the migest package. 另外,我还没有在migest包中更新演示脚本的CRAN版本。 You might be better off using the version on github, https://github.com/gjabel/migest/tree/master/demo , or... now these plots are even easier using the chordDiagram function: 您最好在github上使用该版本, https://github.com/gjabel/migest/tree/master/demo ,或者......现在使用chordDiagram函数可以更轻松地使用这些图:

library("circlize")
m <- read.table("region_reciente.txt", skip=2, stringsAsFactors=FALSE)
#data.frame for details on each region
df1 <- m[,1:3]
names(df1) <- c("order","rgb","region")
n <- nrow(df1)
df1 <- cbind(df1, matrix(as.numeric(unlist(strsplit(df1$rgb,","))),nrow=n, byrow=TRUE) )
names(df1)[ncol(df1)-2:0] <- c("r","g","b")
df1$rcol <- rgb(df1$r, df1$g, df1$b, max = 255)

#flow matrix
m <- m[,-(1:3)]
m <- as.matrix(m)
dimnames(m) <- list(orig=df1$region, dest=df1$region)
chordDiagram(m, directional = TRUE, grid.col=df1$rcol)

在此输入图像描述

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

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