Simplest way to plot changes in ranking between two ordered lists in R?

前端 未结 4 915
深忆病人
深忆病人 2020-12-15 00:04

I\'m wondering if there is an easy way to plot the changes in position of elements between 2 lists in the form of a directed bipartite graph in R. For example, list 1 and 2

4条回答
  •  独厮守ぢ
    2020-12-15 00:19

    Here's a solution using igraph functions.

    rankchange <- function(list.1, list.2){
        grp = c(rep(0,length(list.1)),rep(1,length(list.2)))
        m = match(list.1, list.2)
        m = m + length(list.1)
        pairs = cbind(1:length(list.1), m)
        pairs = pairs[!is.na(pairs[,1]),]
        pairs = pairs[!is.na(pairs[,2]),]
        g = graph.bipartite(grp, as.vector(t(pairs)), directed=TRUE)
        V(g)$color =  c("red","green")[grp+1]
        V(g)$label = c(list.1, list.2)
        V(g)$x = grp
        V(g)$y = c(length(list.1):1, length(list.2):1)
        g
    }
    

    This builds and then plots the graph from your vectors:

    g = rankchange(list.1, list.2)
    plot(g)
    

    enter image description here

    Adjust the colour scheme and symbolism to suit using options detailed in the igraph docs.

    Note this is not thoroughly tested (only tried on your sample data) but you can see how it builds a bipartite graph from the code.

提交回复
热议问题