R:使用segments函数绘制堆叠线图 [英] R: Using the segments function to plot a map of stacked lines

查看:129
本文介绍了R:使用segments函数绘制堆叠线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数沿轴绘制多条线,将它们堆叠在重叠的位置.下面是代码、示例表和它生成的图像.

I have written a function that plots a number of lines along an axis, stacking them where they overlap. Below is the code, a sample table, and the image that it produces.

情节主要是我要找的,但有几件事(按重要性排序):

The plot is mostly exactly what I was looking for but for a few things (in order of importance):

  1. 绘制片段是一个非常缓慢的过程:大约每 0.5 秒绘制 1 个片段.考虑到它们只是线路,我期望更快.我不知道这是什么原因.我知道显式循环在 R 中可能很慢,所以可能是这样,或者我应该以某种方式在屏幕外绘图,然后将绘图推到屏幕上?找到一种节省时间的方法来绘制这种地图很重要,因为我的表格很容易有数万行长.

  1. Plotting the segments is an extremely slow process: about 1 segment every 0.5 seconds. Considering they are just lines I expected something much faster. I don't know the cause of this. I know that explicit loops can be slow in R so it might be this, or should I be plotting off-screen somehow and then pushing the plot to screen afterwards? Finding a time efficient method for plotting this kind of map is important because my tables can easily be tens of thousands of rows long.

我找不到任何方法将 y 位置之间的间隙指定为固定距离,而不管 Y 位置的数量如何.在极端情况下,仅绘制两个段会产生一个彼此相距很远的图.

I can't find any way of specifying the gap between y positions to be a fixed distance regardless of the number of Y positions. At the extreme, plotting just two segments produces a plot with the segments very far apart from each other.

任何人都可以帮助我解决这些问题中的任何一个(或者确实,我可以做得更好的其他任何事情)?

Can anyone help me with either of these points (or indeed, anything else I could be doing better)?

(在这段代码中读取 == 段)

(In this code reads == segments)

功能:

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 -

结果:

推荐答案

抱歉,我没有时间看一个完整的示例,但是 segments()(以及其他函数,像 polygons()points() 等可以将它们的参数作为向量,这样您就可以在一个函数调用中完成所有绘图.通常准备参数在绘图之前(apply()ing 或必要时循环)比重复调用这些绘图函数要快得多.这篇文章中的答案:使用 R 和轴 break() 绘制相当复杂的图表 给出了一个完整的例子这种方法.你一定能够将它应用到你的情况.祝你好运!(感谢你告诉我回答)

Sorry, I don't have time for a worked-through example, but segments() (as well as other functions, like polygons(), points(), etc can take their arguments as vectors, such that you can do all of your drawing in a single function call. Often preparing the arguments prior to plotting (apply()ing or looping if necessary) can be way faster than repeated calls to these drawing functions. The answer in this post: Plotting a rather complex chart using R and axis break() gives a complete example of this approach. You'll definitely be able to apply this to your situation. Good luck! (and thanks for telling me to answer)

这篇关于R:使用segments函数绘制堆叠线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆