【问题标题】:R Circlize "Detect some gaps are too large"R Circlize“检测一些间隙太大”
【发布时间】:2014-05-28 15:55:41
【问题描述】:

我想使用我们的数据制作一个类似的图表发布here,但是,我收到了这个错误消息“检测到一些差距太大”。您是否认为是因为某些值与其他值相比非常小(例如;1 与 1812)?我通过在 1 或 2 之后添加几个零,对矩阵 2 中的数据进行了一些更改,它可以工作。有什么方法可以解决这个范围的数据吗?我想用我的真实数据(矩阵 1)绘制这个漂亮的图表。非常感谢任何帮助。

library(circlize)
#matrix 1
#level0 <- c(1, 8, 39, 14, 2)
#level1 <- c(1, 19, 153, 93, 1)
#level2 <- c(2, 19, 274, 46, 13)
#level3 <- c(0, 8, 152, 1812, 465)
#level4 <- c(0, 2, 1, 164, 226)

#matrix 2
#level0 <- c(100,8,39,14,200)
#level1 <- c(100,190, 153,93,100)
#level2 <- c(200,19,274,646,130)
#level3 <- c(200,800,152,1812,465)
#level4 <- c(200,200,100,164,226)

#build matrix 2
a <- list(c(100,8,39,14,200),c(100,19, 153,93,100), c(200,19,274,646,13),    c(200,8,152,1812,465),c(200,200,100,164,226))
mat <- do.call(rbind, a)
#mat = matrix(sample(1:100, 25, replace = TRUE), 5, 5)
rownames(mat) = c("level 0", "level 1", "level 2", "level 3", "level 4")
colnames(mat) = c("Level0", "Level1", "Level2", "Level3", "Level4")
rn = rownames(mat)
cn = colnames(mat)

factors = c(rn, rev(cn))
factors = factor(factors, levels = factors)
col_sum = apply(mat, 2, sum)
row_sum = apply(mat, 1, sum)
xlim = cbind(rep(0, 10), c(row_sum, col_sum))

par(mar = c(1, 1, 1, 1))
circos.par(cell.padding = c(0, 0, 0, 0), clock.wise = FALSE, track.margin=c(0,0.1),
           gap.degree = 4, start.degree =90)
circos.initialize(factors = factors, xlim = xlim
                  , sector.width = c(row_sum/sum(row_sum), col_sum/sum(col_sum)))
circos.trackPlotRegion(factors = factors, ylim = c(0, 1), bg.border = NA,
                       # bg.col = c("red", "orange", "yellow", "green", "blue", rep("grey", 5)), track.height = 0.05,
                       bg.col = c(c("red", "orange", "yellow", "green", "blue"),
                                  c("blue", "green", "yellow", "orange", "red")), track.height = 0.05,
                       panel.fun = function(x, y) {
                         sector.name = get.cell.meta.data("sector.index")
                         xlim = get.cell.meta.data("xlim")
                         circos.text(mean(xlim), 3, sector.name, adj = c(0.5, 0))
                         circos.axis(labels.cex=0.8, direction="outside", labels.away.percentage=0.5)
                         if(sector.name %in% rn) {
                           for(i in seq_len(ncol(mat))) {
                             circos.lines(rep(sum(mat[sector.name, seq_len(i)]), 2), c(0, 1),
                                          col = "white")
                           }
                         } else if(sector.name %in% cn) {
                           for(i in seq_len(nrow(mat))) {
                             circos.lines(rep(sum(mat[ seq_len(i), sector.name]), 2), c(0, 1),
                                          col = "white")
                           }
                         }
                       })
col = c("#FF000020", "#FFA50020", "#FFFF0020", "#00FF0020", "#0000FF20")
for(i in seq_len(nrow(mat))) {
  for(j in seq_len(ncol(mat))) {
    circos.link(rn[i], c(sum(mat[i, seq_len(j-1)]), sum(mat[i, seq_len(j)])),
                cn[j], c(sum(mat[seq_len(i-1), j]), sum(mat[seq_len(i), j])),
                col = col[i], border = "white")
  }
}

【问题讨论】:

  • 如果更改 gap.degree=0.1,您也可以从代码中获取图表

标签: r plot circos circlize


【解决方案1】:

所以我认为您的df1 对象与我的原始代码有点不同。如果你设置矩阵mdf1 这样......

m <- matrix(c(1, 8, 39, 14, 2, 
              1, 19, 153, 93, 1,
              2, 19, 274, 46, 13,
              0, 8, 152, 1812, 465,
              0, 2, 1, 164, 226), nrow=5, byrow=TRUE)
df1 <- data.frame(order=1:5, region=paste0("level",1:5), 
              rcol = c("red", "orange", "yellow", "green", "blue"),
              lcol = c("#FF000020", "#FFA50020", "#FFFF0020", "#00FF0020", "#0000FF20"),
              stringsAsFactors=FALSE)
df1$region <- factor(df1$region, levels=df1$region)
df1$xmin <- 0
df1$xmax <- rowSums(m)+colSums(m)
n <-nrow(df1)

dimnames(m) <- list(orig=df1$region,dest=df1$region)

你得到以下对象...

> df1
  order region   rcol      lcol xmin xmax
1     1 level1    red #FF000020    0   68
2     2 level2 orange #FFA50020    0  323
3     3 level3 yellow #FFFF0020    0  973
4     4 level4  green #00FF0020    0 4566
5     5 level5   blue #0000FF20    0 1100
> addmargins(m)
        dest
orig     level1 level2 level3 level4 level5  Sum
  level1      1      8     39     14      2   64
  level2      1     19    153     93      1  267
  level3      2     19    274     46     13  354
  level4      0      8    152   1812    465 2437
  level5      0      2      1    164    226  393
  Sum         4     56    619   2129    707 3515

我在working paper 中更详细地解释了df1 的用途。简而言之,df1 对象包含有关要绘制的扇区长度的信息(xminxmax)以及外部圆形矩形的颜色rcol 和色带链接颜色lcol。您当然可以使用相同的 lcolrcol,...适应直到获得您喜欢的调色板/样式(lcol 的透明度可能会降低一些)。

然后您可以继续使用与我在migest package 中的演示文件中的代码非常相似的代码来获取绘图(我只更改了circos.axis 轴参数和df2 的子集)。 ..

library(circlize)
library(plyr)
par(mar=rep(0,4))
circos.clear()

#basic circos graphic parameters
circos.par(cell.padding=c(0,0,0,0), track.margin=c(0,0.15), start.degree = 90, gap.degree =4)

#sector details
circos.initialize(factors = df1$region, xlim = cbind(df1$xmin, df1$xmax))

#plot sectors
circos.trackPlotRegion(ylim = c(0, 1), factors = df1$region, track.height=0.1,
 #panel.fun for each sector
 panel.fun = function(x, y) {
   #select details of current sector
   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")

   #plot labels
   circos.text(x=mean(xlim), y=2.2, labels=name, facing = "arc", cex=0.8)

   #plot main sector
   circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2], ytop=ylim[2], col = df1$rcol[i], border=df1$rcol[i])

   #blank in part of main sector
   #circos.rect(xleft=xlim[1], ybottom=ylim[1], xright=xlim[2]-rowSums(m)[i], ytop=ylim[1]+0.3, col = "white", border = "white")

   #white line all the way around
   #circos.rect(xleft=xlim[1], ybottom=0.3, xright=xlim[2], ytop=0.32, col = "white", border = "white")

   #plot axis
   circos.axis(labels.cex=0.6, major.at=seq(from=0,to=floor(df1$xmax)[i],by=500), 
                                 labels.away.percentage = 0.15)
})

##
##plot links
##
#add sum values to df1, marking the x-position of the first links out (sum1) and in (sum2). Updated for further links in loop below.
df1$sum1 <- colSums(m)
df1$sum2 <- numeric(n)

#create a data.frame of matrix sorted by element size, to allow largest plotted first
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))

#loose non zero links
df2 <- subset(df2, m>0)

#plot links
for(k in 1:nrow(df2)){
  #i,j reference of flow matrix
  i<-match(df2$orig[k],df1$region)
  j<-match(df2$dest[k],df1$region)

  #plot link
  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])

  #update sum1 and sum2 for use when plotting the next link
  df1$sum1[i] = df1$sum1[i] + abs(m[i, j])
  df1$sum2[j] = df1$sum2[j] + abs(m[i, j])
}

这给出了这样的情节......

如果您想为绘图添加一些方向性,请取消注释 panel.fun 中添加白色矩形和边界线的两条线。

【讨论】:

  • 我实际上想要一个单向流,它不允许行和列的名称相同。我也想重现上面的图表,但它确实对我有用。我复制并粘贴了您的代码,但不知何故它不一样。您是否更改了 circos.asis 和 df2 之外的其他参数。感谢您的所有帮助。
  • 我认为你的图表看起来更好。我们可以在每个级别内绘制它们,而不是在马戏团上绘制相同级别的缎带。
  • @Anthony 我的错,我在第一个 circos.rect 参数中有一个参数 top 而不是 ytop。现在应该工作吗?我还注释掉了与彩色矩形重叠的白色矩形......以摆脱方向性。
  • 这很棒。我刚刚复制了你的图表,它看起来很棒。再次感谢您的帮助。
  • @gjabel 谢谢 - 一件额外的事情。显然,“面对”不能将“弧”作为选项。我收到这样的错误消息:“arg”应该是“内部”、“外部”、“逆时针”、“顺时针”、“向下”、“弯曲”......使用其中任何一种虽然有效。
【解决方案2】:

太棒了!这是我为得到下图所做的工作: - 改变 gap.degree=0.1 - 将数字转换为百分比 (mat

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多