2016-09-15 139 views
0

是否可以在顯示log10值的circlize包中創建和絃圖?到目前爲止,我已經能夠產生具有正確尺寸鏈接的圖表,但相應的軸線不匹配。該軸顯示每個扇區的所有鏈接/記錄值的總和,這是不正確的,因爲彙總記錄的值不對應於總和的原始值。有沒有辦法解決這個軸問題?下面R使用對數刻度的circlize和絃圖

是我迄今

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)) 

    } 
) 

這給 the following plot

回答

1

我認爲這是不可能解決這個問題的嘗試的例子。由於扇區由多個鏈路組成,如果扇區的大小是對數變換的,每個鏈路的寬度意味着什麼?我認爲我們應該更好地忘記每個部門的規模,不要顯示軸線。另一方面,我們可以直接在每個鏈接下面或上面顯示未記錄轉換的值。

在下面的代碼中,實際上,chordDiagram()返回一個包含每個鏈接位置的數據框,然後我們可以使用這些信息將未記錄的值添加到正確的位置。

另外請注意代碼中chordDiagram()的第一個參數是錯誤的。我糾正了它。

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) 
} 

enter image description here