从R中的选项卡式文本创建树形图

时间:2014-07-23 17:20:25

标签: r flowchart

我想制作一个下面数据的树/流程图,这些数据使用标签正确缩进:

Vertebrates
    fish
        goldfish
        clownfish
    amphibian
        frog
        toad
    reptiles
        snake
        lizard
        turtle
        tortoise
    birds
        sparrow
        crow
        parrot
    mammals
        dog
        cat
        horse
        whale

如何将此树数据转换为流程图(箭头从上向下或从左向右)(通过计算每行中的标签数来确定正确的位置)。我相信它可以用"图表"包(Graph flow chart of transition from stateshttp://cran.r-project.org/web/packages/diagram/index.html)但无法确定具体步骤。谢谢你的帮助。

下面给出了粗略的样本所需输出。文本周围可能有框。

enter image description here


编辑: 理想情况下,它应该是一种灵活的解决方案,以便在添加或删除级别时能够正常工作。例如,添加两种类型的麻雀:

Vertebrates         
    fish        
        goldfish    
        clownfish   
    amphibian       
        frog    
        toad    
    reptiles        
        snake   
        lizard  
        turtle  
        tortoise    
    birds       
        sparrow 
            house
            factory
        crow    
        parrot  
        crane   
    mammals     
        dog 
        cat 
        horse   
        whale   

dat  = structure(list(V1 = c("Vertebrates", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), V2 = c(NA, "fish", NA, NA, "amphibian", NA, NA, "reptiles", 
NA, NA, NA, NA, "birds", NA, NA, NA, NA, NA, NA, "mammals", NA, 
NA, NA, NA), V3 = c(NA, NA, "goldfish", "clownfish", NA, "frog", 
"toad", NA, "snake", "lizard", "turtle", "tortoise", NA, "sparrow", 
NA, NA, "crow", "parrot", "crane", NA, "dog", "cat", "horse", 
"whale"), V4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "house", "factory", NA, NA, NA, NA, NA, NA, NA, NA
)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, 
-24L))
> 

1 个答案:

答案 0 :(得分:4)

使用igraph这是一种相当复杂的方式。我们需要将您的数据安排到两列,fromto,表示来自 - >的箭头

library(zoo)
library(igraph)

# read tab delimited data - keep structure by setting "" to missing
# (would of been great if you had given this in a format easier to use)

dat <- read.table("test.txt", sep="\t", header=FALSE, fill=TRUE, 
                  na.strings="", strip.white=TRUE, stringsAsFactors=FALSE)

head(dat, 7)
#             V1        V2        V3
#1   Vertebrates      <NA>      <NA>
#2          <NA>      fish      <NA>
#3          <NA>      <NA>  goldfish
#4          <NA>      <NA> clownfish
#5          <NA> amphibian      <NA>
#6          <NA>      <NA>      frog
#7          <NA>      <NA>      toad

准备数据到图表

# carry forward the last value in first two columns to impute missing
dat[1:2] <- sapply(dat[1:2], na.locf, na.rm=FALSE)
dat <- na.omit(dat)

# get edges for graph - we want two columns (from and to) for each edges
edges <- rbind(dat[1:2],setNames(dat[2:3],names(dat[1:2])))

# create graph
g <- graph.data.frame(edges)

# Plot graph
E(g)$curved <- 0
plot.igraph(g, vertex.size=0, edge.arrow.size=0 ,
                      layout=-layout.reingold.tilford(g)[,2:1])

数据因为有更好的方法可以做到这一点!!

dat <- structure(list(V1 = c("Vertebrates", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), V2 = c(NA, 
"fish", NA, NA, "amphibian", NA, NA, "reptiles", NA, NA, NA, 
NA, "birds", NA, NA, NA, "mammals", NA, NA, NA, NA), V3 = c(NA, 
NA, "goldfish", "clownfish", NA, "frog", "toad", NA, "snake", 
"lizard", "turtle", "tortoise", NA, "sparrow", "crow", "parrot", 
NA, "dog", "cat", "horse", "whale")), .Names = c("V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -21L))


编辑:更新新数据

调用更新的数据dat2

# To prepare the data

# carry forward the last value in columns if lower level (col to the right)
# is non-missing
dat2[1] <- na.locf(dat2[1], na.rm=FALSE)

for(i in ncol(dat2):2)  {
  dat2[[i-1]] <-  ifelse(!is.na(dat2[[i]]), na.locf(dat2[[i-1]], na.rm=F), 
                                                                   dat2[[i-1]])
      }            

# get edges for graph
edges <- rbind(na.omit(dat2[1:2]),
                       do.call('rbind',
                               lapply(1:(ncol(dat2)-2), function(i) 
                                  na.omit(setNames(dat2[(1+i):(2+i)],
                                                         names(dat2[1:2])))))
                         )

然后像以前一样继续,给予

enter image description here