2012-03-07 20 views
1

私は、軸に沿っていくつかの線をプロットする関数を書いて、それらが重なるところに積み重ねています。以下は、コード、サンプルテーブル、およびそれが生成するイメージです。R:セグメント関数を使って積み重ねた線のマップをプロットする

プロットは、ほとんどがまさに私が(重要度の順に)のためではなく、いくつかのものを探していたものです。約1セグメントごとに0.5秒:

  1. セグメントをプロットすることは非常に遅いプロセスです。それらがちょうどラインであることを考慮すると、私はもっと速く何かを期待した私はこれの原因を知らない。明示的なループがRで遅くなる可能性があることを知っています。そうかもしれませんか、何とかオフスクリーンでプロットしてから、プロットを後でスクリーンにプッシュする必要がありますか?この種のマップをプロットするための時間効率の良い方法を見つけることは、私のテーブルが簡単に数万行になる可能性があるため重要です。

  2. Y位置の数に関係なく、y位置の間隔を固定距離に指定する方法はありません。極端な場合、2つのセグメントをプロットすると、セグメントが互いに非常に離れているプロットが生成されます。

これらの点のいずれか(または実際には、私がより良くなる可能性のあるもの)を教えてもらえますか?

(このコードで==セグメントを読み出し)

機能:

viewReads <- function(reads){ 
    # sort by start 
    sorted <- reads[order(reads$start),]; 

    #--- 
    # In the first iteration we work out the y-axis 
    # positions that segments should be plotted on 
    # segments should be plotted on the next availible 
    # y position without merging with another segment 
    #--- 
    yread <- c(); #keeps track of the x space that is used up by segments 

    # get x axis limits 
    minstart <- min(sorted$start); 
    maxend <- max(sorted$end); 

    # initialise yread 
    yread[1] <- minstart - 1; 
    ypos <- c(); #holds the y pos of the ith segment 

    # for each read 
    for (r in 1:nrow(sorted)){ 
     read <- sorted[r,]; 
     start <- read$start; 
     placed <- FALSE; 

     # iterate through yread to find the next availible 
     # y pos at this x pos (start) 
     y <- 1; 
     while(!placed){ 

      if(yread[y] < start){ 
       ypos[r] <- y; 
       yread[y] <- read$end; 
       placed <- TRUE; 
      } 

      # current y pos is used by another segment, increment 
      y <- y + 1; 
      # initialize another y pos if we're at the end of the list 
      if(y > length(yread)){ 
       yread[y] <- minstart-1; 
      } 
     } 
    } 

    # find the maximum y pos that is used to size up the plot 
    maxy <- length(yread); 
    sorted$ypos <- ypos; 

    # Now we have all the information, start the plot 
    plot.new(); 
    plot.window(xlim=c(minstart, maxend+((maxend-minstart)/10)), ylim=c(1,maxy)); 
    axis(3); 

    #--- 
    # This second iteration plots the segments using the found y pos and 
    # the start and end values 
    #--- 
    for (r in 1:nrow(sorted)){ 
     read <- sorted[r,]; 
     # colour dependent on strand type 
     if(read$strand == '+'){ 
      color = 'blue' 
     }else{ 
      color = 'red' 
     } 
     #plot this segment! 
     segments(read$start, maxy-read$ypos, read$end, maxy-read$ypos, col=color); 
    } 
} 

サンプルコード:

start end strand 
86 115 + 
87 115 + 
91 116 + 
88 117 + 
91 117 + 
98 125 - 
104 131 + 
104 131 + 
106 132 - 
104 134 + 
104 134 + 
104 134 + 
106 134 + 
106 134 + 
106 134 + 
106 134 + 
106 134 + 
106 135 + 
106 135 + 
106 135 + 
106 135 + 
106 135 + 
106 135 + 
106 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
108 135 + 
109 135 + 
116 135 - 
106 136 + 
106 136 + 
106 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 136 + 
108 137 + 
108 137 + 
109 137 - 
108 138 + 
108 138 + 
108 138 + 
108 138 + 
112 138 + 
112 139 + 
119 141 + 
116 143 + 
121 145 + 
127 145 - 
119 146 + 
121 148 + 
142 169 - 
142 169 - 
160 185 - 
162 185 - 
165 185 - 

結果:

My graph

+1

あなたのコピー+あなたの機能の貼り付けを確認できますか? ' joran

+0

ああ、何かが途中で悪くなった。申し訳ありませんが、私は1分でそれを固定します! – MattLBeck

+1

数日前に私が冷蔵庫に残したサンドイッチに何が起こったかのように聞こえる。 – joran

答えて

2

申し訳ありませんが、私は実際の作業のための時間はありませんが、segments()(そして、polygons()points()などのような他の関数もベクトルとして引数を取ることができます。関数呼び出し。プロットする前に引数を準備することは、これらの描画関数を繰り返し呼び出すよりも高速に実行できます(apply())。この投稿の回答:Plotting a rather complex chart using R and axis break()は、このアプローチの完全な例を示しています。これをあなたの状況に適用することは間違いありません。がんばろう! (お返事をくれてありがとう)

関連する問題