jokergoo / circlize

Circular visualization in R
http://jokergoo.github.io/circlize_book/book/
Other
967 stars 146 forks source link

link.arr.col not working with dataframe (bug?) #53

Closed cardosan closed 7 years ago

cardosan commented 7 years ago

as example if I try to use link.arr.col just get white arrows for all the links using the tutorial as MWE (tried also in my own data with a dataframe passed with same results) :


library(circlize)
# library(gtools)
# library(dplyr)

set.seed(999)
mat = matrix(sample(18, 18), 3, 6)
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
mat

df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)
grid.col = c(S1 = "red", S2 = "green", S3 = "blue",
             E1 = "grey", E2 = "grey", E3 = "grey", E4 = "grey", E5 = "grey", E6 = "grey")

# 
lty_df = data.frame(c("S1", "S2", "S3"), c("E5", "E6", "E4"), c(1, 2, 3))
lwd_df = data.frame(c("S1", "S2", "S3"), c("E5", "E6", "E4"), c(2, 2, 2))
border_df = data.frame(c("S1", "S2", "S3"), c("E5", "E6", "E4"), c(1, 1, 1))
arr.col = data.frame(c("S1", "S2", "S3"), c("E5", "E6", "E4"),
                     c("black", "black", "black"))
# 
chordDiagram(df,
#              # reduce=0,
             # grid.col = grid.col,
             # link.lty = lty_df,
             # link.lwd = lwd_df,
             directional = 2,
             direction.type = c("diffHeight", "arrows"),
             link.arr.col = arr.col,
            #   link.arr.length = 0.2,
            # link.border = border_df
             )

circos.clear()

rplot06

I also get some 50 warnings like:

Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  supplied color is neither numeric nor character

Is it a bug or am I wrong in something?

jokergoo commented 7 years ago

Hi, the support for link.arr.col as a data frame is missing. I only considered it when the first argument is a matrix while I totally forgot to test it when the first argument is a data frame.

Currently, you can set link.arr.col as a color vector which has the same order as df. link.arr.col as a data frame will be supported very soon.

chordDiagram(df,
             directional = 2,
             direction.type = c("diffHeight", "arrows"),
             link.arr.col = c("black", rep("white", nrow(df) - 1))
             )
jokergoo commented 7 years ago

Hi, I have updated the package, now link.arr.col as data frame is supported now.

cardosan commented 7 years ago

indeed it works now, thanks!

akkalbist55 commented 6 years ago

Hi, jokergoo this is my code and there is some problem in fill "follow" in boundary also mow to represent season in separate network flowing line please help me!!

library(circlize)

export_dist <- c("Kailali","Kathmandu","Bardia","Kathmandu","Bardia","Kathmandu","Dolakha","Bajhang","Kailali","Kailali","Bardia","Sagaematha","Kailali","Kathmandu","Kathmandu","Kathmandu","Bardia","Mahendranagar","Bardia","Sanja","Salleri","Jajarkot","Bajura")
import_dist <- c("Butwal","Bardia","Sagarmatha","Butwal","Bajhang","Bajhang","Lalitpur","Kathmandu","Lalitpur","Lalitpur","Mahendranagar","Kailali","Butwal","Kathmandu","Dolakha","Butwal","Lalitpur","Butwal","Lalitpur","Lalitpur","Vojpur","Vojpur","Dolakha")
flow <- c(2000,65780,78010,851,35353,845,738,120788,245900,90002,4426,6870,152681,78114,32591,19274,10915,23100,6275,4544,8750,9800,1349)
season<- c("Spring","Summer","Winter","Autumn","Summer","Winter","Autumn","Spring","Summer","Winter","Autumn","Summer","Winter","Autumn","Summer","Winter","Autumn","Summer","Winter","Autumn","Spring","Summer","Winter")
df <- data.frame(export_dist, import_dist, flow,stringsAsFactors = FALSE)

dist = unique(c(df[[1]], df[[2]]))
color <- c("#E41A1C","#800000","#ff8c00","#ffd700","#008000","#00bfff","#377EB8",
           "#ff69b4","#800080","#4b0082","#000000","#800000","#E8ECE7","#7109D3","#5F4974")

df1 <- data.frame(dist, 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 = dist, 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")
    dist = df1$dist[df1$dist == sector.index]

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

  }
)

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)
}
akkalbist55 commented 6 years ago

this is look like plot_zoom_png 1