R用对数刻度圆化和弦图 [英] R circlize chord diagram with log scale

查看:217
本文介绍了R用对数刻度圆化和弦图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在circlize程序包中制作一个显示log10值的和弦图?到目前为止,我已经能够生成具有正确尺寸链接的图,但是相应的轴将不匹配.该轴显示每个扇区的所有链接/记录值的总和,这是不正确的,因为求和记录值与原始求和值不对应.有什么办法可以解决这个轴问题?

Is it possible to make a chord diagram in the circlize package that displays log10 values? So far I have been able to produce a plot with correct size links, but the corresponding axis will not match up. the axis displays the sum of all links/logged values per sector, which is not correct as summing logged values does not correspond to summed raw values. Is there any way to fix this axis problem?

下面是我到目前为止尝试过的一个例子

below is an example of what I have tried so far

library(circlize)

export_country <- c("DEU","USA","IDN","USA","IDN","USA","IDN","CAN","DEU","DEU","IDN","NZL","DEU","USA","USA","USA","IDN","SGP","IDN")
import_country <- c("JPN","JPN","USA","JPN","TWN","CAN","CHN","USA","CHN","CHN","DEU","JPN","USA","DNK","JPN","CHN","JPN","CHN","CHN")
flow <- c(2000,65780,78010,851,35353,845,738,120788,245900,90002,4426,6870,152681,78114,32591,19274,10915,23100,6275)

df <- data.frame(export_country, import_country, flow,stringsAsFactors = FALSE)


country = unique(c(df[[1]], df[[2]]))
color <- c("#E41A1C","#800000","#ff8c00","#ffd700","#008000","#00bfff","#377EB8",
               "#ff69b4","#800080","#4b0082")

df1 <- data.frame(country, color,stringsAsFactors = FALSE)

circos.clear()
circos.par(start.degree = 90, gap.degree = 5, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
par(mar = rep(0, 4))

chordDiagram(x = df[1:2],log10(df[3]), grid.col = color, transparency = 0.25,
         order = country, directional = 1,
         direction.type = c("arrows", "diffHeight"), diffHeight  = -0.04,
         annotationTrack = c("grid","axis"), annotationTrackHeight = c(0.05, 0.1),
         link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)



circos.trackPlotRegion(
  track.index = 1, 
  bg.border = NA, 
  panel.fun = function(x, y) {
xlim = get.cell.meta.data("xlim")
sector.index = get.cell.meta.data("sector.index")
country = df1$country[df1$country == sector.index]

circos.text(x = mean(xlim), y = 4.4, 
            labels = country, facing = "bending", cex = 1, niceFacing = TRUE, adj = c(0.5, 0))

  }
 )

给出 以下情节

推荐答案

我认为无法解决此问题.由于一个扇区由几个链接组成,因此如果将扇区的大小进行对数转换,那么每个链接的宽度是什么意思?我认为我们最好不要忘记每个部门的规模,而不要显示轴.另一方面,我们可以直接在每个链接的上方或上方显示未经对数转换的值.

I think it is impossible to solve this problem. Since a sector is composed by several links, if the size of the sector is log-transformed, what does the width of each link mean? I think we should better forget the scale of each sector and do not show the axes. On the other hand, we can directly show the un-log transformed value beneath or above each link.

实际上,在下面的代码中,chordDiagram()返回一个包含每个链接位置的数据框,然后我们可以使用此信息将未记录的值添加到正确的位置.

In following code, actually, chordDiagram() returns a data frame which contains positions of each link, then we can use this information to add un-logged values just to the right place.

还请注意,代码中chordDiagram()中的第一个参数是错误的.我纠正了.

Also please note the first argument in chordDiagram() in your code was wrong. I corrected it.

library(circlize)

export_country <- c("DEU","USA","IDN","USA","IDN","USA","IDN","CAN","DEU","DEU","IDN","NZL","DEU","USA","USA","USA","IDN","SGP","IDN")
import_country <- c("JPN","JPN","USA","JPN","TWN","CAN","CHN","USA","CHN","CHN","DEU","JPN","USA","DNK","JPN","CHN","JPN","CHN","CHN")
flow <- c(2000,65780,78010,851,35353,845,738,120788,245900,90002,4426,6870,152681,78114,32591,19274,10915,23100,6275)

df <- data.frame(export_country, import_country, flow,stringsAsFactors = FALSE)
df[[3]] = log10(df[[3]])

country = unique(c(df[[1]], df[[2]]))
color <- c("#E41A1C","#800000","#ff8c00","#ffd700","#008000","#00bfff","#377EB8",
               "#ff69b4","#800080","#4b0082")

df1 <- data.frame(country, color,stringsAsFactors = FALSE)

circos.clear()
circos.par(start.degree = 90, gap.degree = 5, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
par(mar = rep(0, 4))

res = chordDiagram(x = df, grid.col = color, transparency = 0.25,
         order = country, directional = 1,
         direction.type = c("arrows", "diffHeight"), diffHeight  = -0.04,
         annotationTrack = c("grid"), annotationTrackHeight = c(0.05, 0.1),
         link.arr.type = "big.arrow", link.sort = TRUE, link.largest.ontop = TRUE)



circos.trackPlotRegion(
  track.index = 1, 
  bg.border = NA, 
  panel.fun = function(x, y) {
    xlim = get.cell.meta.data("xlim")
    sector.index = get.cell.meta.data("sector.index")
    country = df1$country[df1$country == sector.index]

    circos.text(x = mean(xlim), y = 1.5, 
                labels = country, facing = "bending", adj = c(0.5, 0), cex = 1, niceFacing = TRUE)

      }
 )

for(i in seq_len(nrow(res))) {
  circos.text(x = res$x1[i] - res$value[i]/2, y = 0.5, round(10^(res$value[i])), facing = "inside",
      niceFacing = TRUE, adj = c(0.5, 0.5), cex = 0.5, col = "white", sector.index = res$rn[i], track.index = 1)
  circos.text(x = res$x2[i] - res$value[i]/2, y = 0.5, round(10^(res$value[i])), facing = "inside",
      niceFacing = TRUE, adj = c(0.5, 0.5), cex = 0.5, col = "white", sector.index = res$cn[i], track.index = 1)
}

这篇关于R用对数刻度圆化和弦图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆