#------------------------------------------------------------------------------------- # File name : tas_diamondgraph_figure3.ssc # Date : 12/15/2005 (Version 2) # Authors : Xiuhong Li and Alvaro Muņoz # # Figure 3 in Li X, Buechner J, Tarwater P, Muņoz A. A Diamond-Shaped # Equiponderant Graphical Display of the Effects of Two Categorical Predictors on # Continuous Outcomes. The American Statistician, August 2003, Vol 57, No. 3, 193-199. # # Figure legend: Two-dimensional approach for depicting the likelihood of developing # AIDS in six years according to the amount of HIV in the blood and the # degree of immune deficiency. Cells with no or very few individuals # are blank. # # To run code provided, you must have a license to run S-plus. Authors used code to # produce figures in the paper in The American Statistician using S-plus 6.0. # Recipients of code may need to do changes to use in other versions and for # different applications. #------------------------------------------------------------------------------------- #------------------------------------------------------------------------------------- # 1. Obtain the number of categories of the two predictors; # row = number of categories of the predictor on the southwest side of Diamond Graph; # col = number of categories of the predictor on the southeast side of Diamond Graph; #------------------------------------------------------------------------------------- row <- 5 col <- 5 #------------------------------------------------------------------------------------- # 2. Select a square region for plot; #------------------------------------------------------------------------------------- par(pty="s") #------------------------------------------------------------------------------------- # 3. Assign the coordinate of the lowest point of Diamond Graph to (0,0), and define # the ranges for x and y axes based on the number of categories of two predictors. #------------------------------------------------------------------------------------- plot(0,0, xlim=c(0-row, col), ylim=c(0,row+col),axes=F, type="n", xlab="", ylab=""); #------------------------------------------------------------------------------------- # 4. Obtain the coordinate of the center of each square cell (cx,cy) with diagonal= 2; # i.e., the distance from the center to a vertex of a square cell equals to 1. #------------------------------------------------------------------------------------- for (i in 1:row) { if (i == 1){ cx <- seq(0,(col-1) ) cy <- seq(1,col) } else{ cx <- c(cx, seq( (1-i),(col-i) ) ) cy <- c(cy, seq(i,(col+i-1)) ) } } #------------------------------------------------------------------------------------- # 5. Read in the outcomes, which start from the value in the cell 1 (at bottom) # to cell 2, to cell 3, and finally to cell 4 (at top), as depicted below in # the 2x2 graph. # /4\ <- top # / \/\ # \3/\2/ # \1/ <- bottom # # Empty cells (with no subjects) are assigned -1 and will not be displayed. # Only positive outcomes will be plotted as polygons in square cells. #------------------------------------------------------------------------------------- outcome <- c(.02,.1, -1, -1, -1, .17,.17,.17, -1, -1, .14,.37,.37,.37, -1, .37,.55,.55,.73,.73, .67,.67,.78,.89,.98) #------------------------------------------------------------------------------------- # 6. If data are not proportional, convert the outcomes to proportions - # each value is divided by the highest one within that outcome. # If data are proportional, no need to convert, which is the case for Figure 3. #------------------------------------------------------------------------------------- p <- outcome #------------------------------------------------------------------------------------- # 7. Calculate the 'scale' based on the number of categories of two predictors; # We have found scaling all proportions (p) by 'scale' useful in the # graphical illustration when p is high. #------------------------------------------------------------------------------------- scale <- 1.005 - 0.005*(row+col) p[p<1] <- p[p<1] *scale p[p==1] <- p[p==1]*scale+0.005 #------------------------------------------------------------------------------------- # 8. Draw the outline of the square cells. # 1) draw 45 degree lines from southwest to northeast # 2) draw -45 degree lines from southeast to northwest #------------------------------------------------------------------------------------- for (i in 0:row) lines(c(-i,col-i),c(i,col+i),lwd=0.01) for (i in 0:col) lines(c(i, -row+i), c(i, row+i),lwd=0.01) #------------------------------------------------------------------------------------- # 9. Calculate the coordinates of the six vertices of inside polygons; # start clockwise from northwest vertex as (x1, y1). # x1,y1 _____ x2,y2 # / \ # x6,y6 / \ x3,y3 # \ / # \_____/ # x5,y5 x4,y4 #------------------------------------------------------------------------------------- x1<- cx - (1-p)/2 x2<- cx + (1-p)/2 x3<- cx + (1+p)/2 x4<- cx + (1-p)/2 x5<- cx - (1-p)/2 x6<- cx - (1+p)/2 y1<- cy + p y2<- cy + p y3<- cy y4<- cy - p y5<- cy - p y6<- cy #------------------------------------------------------------------------------------- # 10. Plot a polygon inside each square cell, and then print the value of the outcome # at the center in each square cell. # # Here we use cex= 0.7*scale for the labels. Other applications may # need to use a different factor so that labels can be read easily. #------------------------------------------------------------------------------------- n <- row*col for (i in 1:n) { if (p[i]>0) polygon(c(x1[i],x2[i],x3[i],x4[i],x5[i],x6[i]), c(y1[i],y2[i],y3[i],y4[i],y5[i],y6[i]), density=-.1, border=F, col=5) if (outcome[i]>=0){ # In Windows system, S-plus needs 0.03 to be added to cy so that lable is properly centered text(cx[i], cy[i]+0.03, outcome[i]*100, cex=0.7*scale, lwd=2) } } #------------------------------------------------------------------------------------- # 11. Below are the commands to print labels for two predictors. # It requires trial and error for the proper location; # but users can skip the following steps and add the labels in PowerPoint. #------------------------------------------------------------------------------------- # print labels on the southwest side of Diamond Graph text(c(-5.4,-5.4), c(1.8,1.8), adj=0, cex=.9,"Amount of HIV") text(c(-5.0,-5.0), c(1.5,1.5), adj=0, cex=.9,"in Blood") text(c(-0.7,-0.7), c(.35,.35), adj=1, cex=.9,"very low") text(c(-1.7,-1.7), c(1.35,1.35), adj=1, cex=.9,"low") text(c(-2.7,-2.7), c(2.35,2.35), adj=1, cex=.9,"medium") text(c(-3.7,-3.7), c(3.35,3.35), adj=1, cex=.9,"high") text(c(-4.7, -4.7), c(4.35,4.35), adj=1, cex=.9,"very high") # print labels on the southeast side of Diamond Graph text(c(4.0, 4.0), c(1.8,1.8), adj=0, cex=.9,"Degree of Immune") text(c(4.5, 4.5), c(1.5,1.5), adj=0, cex=.9,"Deficiency") text(c(0.7, 0.7), c(.35,.35), adj=0, cex=.9,"minor") text(c(1.7, 1.7), c(1.35,1.35), adj=0, cex=.9,"mild") text(c(2.7, 2.7), c(2.35,2.35), adj=0, cex=.9,"moderate") text(c(3.7, 3.7), c(3.35,3.35), adj=0, cex=.9,"severe") text(c(4.7, 4.7), c(4.35,4.35), adj=0, cex=.9,"very severe")