[R] plotting missing data patterns

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Jan 6 12:21:06 CET 2002


"Patrick E. McKnight" wrote:
> 
> I have one other problem and then I'll stop and get back to writing.  I
> want to plot a missing data matrix (the R matrix a la Little and Rubin)
> to graphically depict where the missing data lie in the matrix.  Some
> statistical packages produce a graphical depiction of the missing data
> patterns by plotting the matrix, color-coding the plot with contrasting
> colors for either missing or present.
> 
> So far, I have fumbled my way through parts of it but I can't figure out
> what I am doing wrong.  Here is my situation....
> 
> Dataset...now sorted the way I want it (thanks)
> 
>  > newcouple
>     id age sex iv1 iv2 dv mdpf
> 2   0   0   0   0   0  0    1
> 3   0   0   0   0   0  0    1
> 5   0   0   0   0   0  0    1
> 7   0   0   0   0   0  0    1
> 9   0   1   0   0   0  0    3
> 6   0   0   1   0   0  0    5
> 10  0   0   0   1   0  0    9
> 1   0   0   0   0   1  0   17
> 4   0   0   0   0   0  1   33
> 8   0   0   0   0   0  1   33
> 
> # begin code
> 
> # create matrix from data.frame for easy indexing
> 
>  > dummy <- as.matrix(newcouple)
> 
> # create new vectors to later plot
> 
> x <- mat.or.vec(1,1)
> y <- mat.or.vec(1,1)
> 
> # define default plotting information with par
> 
> par(bg="black",pch=22)
> 
> # create new frame for plotting
> 
> plot.new()
> 
> # loop through the matrix to only plot a square for the observations and
> # variables that are missing
> 
>      for (i in 1:nrow(dummy)){
>        for (j in 1:ncol(dummy)){
>          if (dummy[i,j] == 1){
> 
> # record the coordinates for the missing values to plot with a points
> # call later
> 
>             y <- append(y,i)
>             x <- append(x,j)
>           }
>        }
>      }
> 
> # points to plot from my
> 
>      points(x,y,bg="white")
> 
> # end code
> 
> The graphical output is just a black window with one point plotted (0,0
> I think).  What am I doing wrong?  

You did not set the dimensions in plot.window() after calling
plot.new().

I would suggest to use image() instead of your code:

 dummy <- as.matrix(newcouple)
 image(x = 1:nrow(dummy), y = 1:ncol(dummy), z = dummy,
     breaks = c(min(dummy), 0, 1, max(dummy)), col = c(1, 0, 1))


*Your* code can be simplified (loops) and corrected as follows (the
"reversed" colours are the most complicated thing):

 dummy <- as.matrix(newcouple)
 x <- col(dummy)[dummy==1]
 y <- row(dummy)[dummy==1]
 par(bg="black", fg="white", col.axis="white", col.lab="white", 
     col.sub="white", col.main="white", pch=22)
 plot(x, y, type="n")
 points(x,y)



> The points function does not appear
> to be adding the extra points the way I want it to add them to the same
> display window.
> 
> In the end, I would like to have either a black background with white
> squares (holes) or a white background with black squares to represent
> where the data are missing.  I will adjust the squares to an appropriate
> size later.  I know that there are other procedures for graphically
> depicting missing data but I (and my colleagues) want this graph for now.


Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list