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

前端 未结 4 926
深忆病人
深忆病人 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:29

    With ggplot2:

    v1 <- c("a","b","c","d","e","f","g")
    v2 <- c("b","x","e","c","z","d","a")
    
    o <- 0.05
    DF <- data.frame(x = c(rep(1, length(v1)), rep(2, length(v2))),
                     x1 = c(rep(1 + o, length(v1)), rep(2 - o, length(v2))),
                     y = c(rev(seq_along(v1)), rev(seq_along(v2))),
                     g = c(v1, v2))
    
    library(ggplot2)
    library(grid)
    ggplot(DF, aes(x=x, y=y, group=g, label=g)) +
      geom_path(aes(x=x1), arrow = arrow(length = unit(0.02,"npc")), 
                size=1, color="green") +
      geom_text(size=10) +
      theme_minimal() +
      theme(axis.title = element_blank(),
            axis.text = element_blank(),
            axis.ticks = element_blank(),
            panel.grid = element_blank())
    

    resulting graph

    This can of course be wrapped in a function easily.

提交回复
热议问题