创建使用R显示案例计数的树图

时间:2011-01-18 09:57:44

标签: r data-visualization

我需要创建一个“树图”式图表来显示不同场景的案例数量,如下所示:

图片引自:

Pediatrics. 2005 Dec;116(6):1317-22.
Electronic surveillance system for monitoring surgical antimicrobial prophylaxis.
Voit SB, Todd JK, Nelson B, Nyquist AC.

我可以使用table命令从R轻松获取数字,但这不是一种非常好的方式来呈现它。

图表可以在没有任何花哨的颜色或东西的情况下制作,我只想使用格式来显示数字。有什么建议吗?

3 个答案:

答案 0 :(得分:9)

可以使用“图表”包绘制树形图。它是绘制流程图等的通用包。参见


library(diagram)
demo("flowchart")

答案 1 :(得分:7)

library(diagram)
par(mfrow=c(1,1))
par(mar=c(0,0,0,0))
##initialize new grphics device
openplotmat()
##number of elements per row
elpos<-coordinates (c(1,1, 2, 2, 2, 3,2 ))
##draw arrows from each row to next row
treearrow(from=elpos[1,],to=elpos[2,],lwd=6)  
treearrow(from=elpos[2,],to=elpos[3:4,],lwd=6) 
treearrow(from=elpos[3,],to=elpos[5,],lwd=6)  
treearrow(from=elpos[4,],to=elpos[6,],lwd=6)  

treearrow(from=elpos[5,],to=elpos[7:8,],lwd=6)  

treearrow(from=elpos[7,],to=elpos[9:10,],lwd=6)
treearrow(from=elpos[8,],to=elpos[11,],lwd=6)  

treearrow(from=elpos[9,],to=elpos[12,],lwd=6)  
treearrow(from=elpos[10,],to=elpos[13,],lwd=6)

##create a generic 3-lined label for each textbox
labels = vector(length=13)
for(i in 1:13) { 
labels[i] = paste(c(sample(letters, 3), "\n", sample(letters, 3) , "\n", sample(letters, 3)), collapse="")
}
labels[12] = "Consistent with AAP\nguidelines"

##plot text boxes
for ( i in 1:13) textround (elpos[i,],radx=0.08,rady=0.05,lab=labels[i])

enter image description here

答案 2 :(得分:5)

我每周都会对这些类型的图表提出类似的要求。我做了什么头骨在这里建议,然后我煮了这个。它可能不如你展示的那个好,但它有主要的想法。

consort.dia <- function(
screened=45,
eligible=46,
neligible=47,
interested=48,
ninterested=49,
consented=50,
nconsented=51,
treat=52,
control=53
){
require(diagram)

 openplotmat(main="Consort Diagram")

 elpos<-coordinates (c(1,3,3,4,5))

 fromto <- matrix(ncol=2,byrow=TRUE,
                 data=c(1,2,
                        1,3,
                        1,4,
                        2,5,
                        2,6,
                        2,7,
                        5,8,
                        5,9,
                        5,10,
                        8,12,
                        8,13
                      )
            )

 nr     <-nrow(fromto)

 arrpos <- matrix(ncol=2,nrow=nr)

 for (i in 1:nr) 
     arrpos[i,] <- straightarrow (  
                        to=elpos[fromto[i,2],],
                        from=elpos[fromto[i,1],],
                        lwd=2,arr.pos=0.6,
                        arr.length=0.5
                       )

 textrect   (elpos[1,],radx=.094,rady=.05,lab=paste("Screened\n",screened))

 textrect   (elpos[2,],radx=.094,rady=.05,lab=paste("Eligible\n",eligible))

 textrect   (elpos[3,],radx=.094,rady=.05,lab=paste("Not Eligible\n",neligible))

 textrect   (elpos[4,],radx=.094,rady=.05,lab=paste("Screening \n Incomplete\n",screened-(neligible+eligible)))

 textrect   (elpos[5,],radx=.094,rady=.05,lab=paste("Interested\n",interested))

 textrect   (elpos[6,],radx=.094,rady=.05,lab=paste("Not Interested\n",ninterested))

 textrect   (elpos[8,],radx=.094,rady=.05,lab=paste("Consented\n",consented))

 textrect   (elpos[9,],radx=.094,rady=.05,lab=paste("Not Consented\n",nconsented))

 textrect   (elpos[12,],radx=.094,rady=.05,lab=paste("Treatment\n",treat))

 textrect   (elpos[13,],radx=.094,rady=.05,lab=paste("Control\n",control))

 textrect   (elpos[7,],radx=.094,rady=.05,lab=paste("Unable to \nReach\n",eligible-{interested+ninterested}))

 textrect   (elpos[10,],radx=.094,rady=.05,lab=paste("In Progress\n",interested-{consented+nconsented}))

}

输出: enter image description here