Graph flow chart of transition from states

后端 未结 4 688
忘掉有多难
忘掉有多难 2020-12-07 12:00

I\'m trying to find a way to graph something like this in R:

\"Image

It is a tr

4条回答
  •  佛祖请我去吃肉
    2020-12-07 12:23

    My answer is just a proof of concept to demonstrate the feasibility of this plot using grid and bezier. I use lattice to plot the scene and then grid package in the native. It is just a start, I think you can easily complete it.

    enter image description here

    library(grid)
    library(lattice)
    
    dat <- data.frame(x=c(1,1,2,2),
                      y=c(1,2,1,2),
                      weight=c(2,1,1,2),
                      text=c('B','A','B','A'))
    cols <- colorRampPalette(c("grey", "green"))(nrow(dat))
    xyplot(y~x,data=dat,groups=weight,
           xlim=extendrange(dat$x,f=1),
           ylim=extendrange(dat$y,f=1),
           panel=function(x,y,groups,...){
             lapply(seq_along(x),function(i){
             grid.roundrect(x[i],y[i],
                            width=.5, 
                            height=.5*groups[i],
                            gp=gpar(fill=cols[i],lwd=5,col='blue'),
                            def='native')
             grid.text(x[i],y[i],label=dat$text[i],
                        gp=gpar(cex=5,col='white'),
                       def='native')
            })
             xx <- c(x[1]+0.25, x[1]+0.25, x[3]-0.25, x[3]-0.25)
             yy <- c(y[1], y[1], y[3], y[3])
             grid.bezier(xx, yy,
                         gp=gpar(lwd=3, fill="black"),
                         arrow=arrow(type="closed"),
                         def='native')
             xx <- c(x[1]+0.25, 1, 2, x[4]-0.25)
             yy <- c(y[1], 2, 1, y[4])
             grid.bezier(xx, yy,
                         gp=gpar(lwd=3, fill="black"),
                         arrow=arrow(type="closed",
                                     length=unit(0.5, "inches")),
                         def='native')
             xx <- c(x[2]+0.25, x[2]+0.25, x[4]-0.25, x[4]-0.25)
             yy <- c(y[2], y[2], y[4], y[4])
             grid.bezier(xx, yy,
                         gp=gpar(lwd=3, fill="black"),
                         arrow=arrow(type="closed",
                                     length=unit(0.5, "inches")),
                         def='native')
    
             })
    

提交回复
热议问题