creating tree diagram for showing case count using R
I need to create a "tree diagram"-like graph to present the number of cases for different scenarios, like the one shown below:
The picture is quoted from :
Pediatrics. 2005 Dec;116(6):1317-22.
Electronic surveillance system for monitoring surgical antimicrobial prophylaxis.
Voit SB, Todd JK, Nelson B, Nyquist AC.
I can get the numbers easily from R开发者_开发问答 using the table
command, but it is not a very good way to present it.
The chart can be made without any fancy colors or stuff, I just want to use the format to present the numbers. Any suggestions?
The tree diagram could be drawn using the "diagram" package. It is a generic package for drawing flow diagrams etc. See
library(diagram)
demo("flowchart")
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])
I had an similar request for these types of charts every week. I did what skullkey suggests here and then I cooked up this. It probably is not as nice as the one you show, but it has the main idea.
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}))
}
Outputs this:
精彩评论