QBoard » Advanced Visualizations » Viz - Others » Transition plot in R how does it work?

Transition plot in R how does it work?

  • I am trying to visualise a transition from one state (work in origin country) and another (work in destination country). I think transition plot is perfect but I can't get how does it work?

    My data frame is called (Cimad) and my variables are EMPLOIPRE (origin) and EMPLOIFR (destination). Both are factors, the first count 20 levels, the second 15 levels (is it a problem, do I have to make them all match?) and I have 400 observations.

    I hope you'll be able to help me because I am completely lost ! Thank you in advance

      August 25, 2020 4:02 PM IST
    0
  • The problem with Sankey diagram in your case is that you will duplicate all the nodes (on the left, and on the right), that is messy.

    Networks are better, but nodes are not well structured.

    I suggest to try chord diagrams for such data.


    One of the greatest examples of chord diagram was build with d3.js, and based on migration flows dataset. Here is an R wrapper for d3.js chords. Or you can try to reproduce this manual.

    This post was edited by Rakesh Racharla at August 25, 2020 4:43 PM IST
      August 25, 2020 4:04 PM IST
    0
  • Fwiw, here's an example

    # create example data
    set.seed(1)
    Cimad <- data.frame(
      EMPLOIPRE = as.factor(sample(letters[1:15], 400, T)),
      EMPLOIFR = as.factor(sample(letters[1:20], 400, T))
    )
    
    # create adjacency/transition matrix
    labs <- unique(unlist(Cimad))
    m <- table(factor(Cimad[,1], lev=labs), factor(Cimad[,2], lev=labs))
    m <- m / rowSums(m)
    
    # plot it
    library(igraph)
    g <- graph_from_adjacency_matrix(m, weighted = "prob")
    E(g)$prob <- ifelse(is.nan(E(g)$prob), NA, E(g)$prob)
    plot(g, edge.label = round(E(g)$prob, 2), edge.arrow.size = .25, edge.label.cex = .5)​


     
    Or, if you want to export it into a PDF (e.g. for zoom):

    pdf(tf <- tempfile(fileext = ".pdf"), width = 15, height = 15)
    plot(g, edge.label = round(E(g)$prob, 2), edge.arrow.size = .25, edge.label.cex = .5)
    dev.off()
    shell.exec(tf)​
      August 25, 2020 4:09 PM IST
    0
  • You can use plotmat function from the diagram package.

    If you are not installed, let’s install the package.

     

    install.packages("diagram")
    library(diagram)
    plotmat(transition_matrix[1:3,1:3])​
     
    transition diagram in r
      August 24, 2021 2:14 PM IST
    0