Arrangement of large number of plots and connect with lines in r

前端 未结 2 490
误落风尘
误落风尘 2021-01-31 05:58

I have a large number of small plots need to placed in a bigger plot canvase and arrange small plots into and connect them with lines. A small example will look like this:

2条回答
  •  有刺的猬
    2021-01-31 06:13

    EDIT after bounty start:

    • Chage how to compute the coordinates of lines: no need to use merge
    • Change how to draw teh joined lines: pretty connected lines.

    enter image description here

    First of all I need to transform your connected data from points labels to coordinated points (x,y)

    ## here the edit 
    dat.lines <- do.call(cbind,apply(connectd,2,
                                     function(x){
                                       id <- match(x,plotcord$plotname)
                                       plotcord[id,c(2,3)]}))
    
    colnames(dat.lines) <- paste(rep(c('x','y'),3),rep(1:3,each=2),sep='')
    

    This is how it looks my dat.lines :

         x1 y1   x2 y2   x3 y3
    1 1.750  2 1.50  3 2.00  3
    2 5.250  2 5.00  3 5.50  3
    3 1.375  1 1.00  2 1.75  2
    4 3.500  1 1.75  2 5.25  2
    5 6.000  1 1.75  2 5.25  2
    6 7.500  1 5.25  2 8.00  2
    

    Then , I plot the points using lattice xyplot. The use of lattice is really suitable for such plots. No need to sacle the data (as grid package for example). Then I customize the panel adding rectangle, segments,...

    library(latticeExtra))
    xyplot(plotgridY~plotgridX,data= plotcord,
           panel=function(x,y,...){
         apply(dat.lines,1,function(x){
           panel.segments(x0=x['x2'],y0=x['y2'],x1=x['x3'],y1=x['y3'])
           boxh <- 0.5
           x1=x['x1']
           y1=x['y1']
           y2 <- x['y2']
           x2 <- (x['x2']+x['x3'])/2
           ydelta <- (y2 - y1)/2
           browser()
           panel.segments(c(x1, x1, x2), c(y1, y1 + ydelta, y2 - 
                                       ydelta), c(x1, x2, x2), c(y1 + ydelta, y2 - 
                                                                   ydelta, y2))
         })
    
             panel.rect(x=x,y=y,width=unit(2,'cm'),
                        height=unit(2,'cm'),col='lightyellow')
             panel.xyplot(x,y,...)
             panel.text(x,y,adj=c(0,-3),
                        label=plotcord$plotname,cex=1.5)
    
             ## add some prove of concept detail 
             panel.rect(x=x,y=y,width=unit(0.5,'cm'),
                        height=unit(0.5,'cm'),col='lightblue',lty=2)
             panel.text(x,y,adj=c(1,2),
                        label=paste(plotcord$plotname,1,sep=''),cex=1,col='blue')
             panel.text(x,y,adj=c(-0.5,2),
                        label=paste(plotcord$plotname,2,sep=''),
                        cex=1,col='blue')
    
    
             },ylim=extendrange(plotcord$plotgridY,f=0.5),xlab='',ylab='', axis = axis.grid,
       main='Arrangement of large number of plots \n and connect with lines ')
    

提交回复
热议问题