Graphics binary variable in R

I would like to build simple graphics. I have a dat set with n rowns and k columns in which each row has a sequence of 0 and 1. I would like to build exactly this sequence for all rows.

Actually, I want to reproduce the number 24.1, p. 516, books by Gelman and Hill (Data aAnalysis using regression and multilevel / hierarchical models). I suspect that he made latex graphics, but it seems pretty funny that I cannot repeat this simple graphics in R. A figure is something like this . As can be seen from the link, “those” are replaced by “S” and “zeros” by “.”. This is a simple graphic, but it shows each individual answer in time.

+3
source share
3 answers

I would go with formatted text using sprintf. Much cleaner and simpler. If you still want a plot, you can go with the following:

This matrix tblcontains your data:

tbl <- matrix(data=rep(0:1,25), nrow=5)

You can generate a chart as:

plot(1, 1, xlim=c(1,dim(tbl)[2]+.5), ylim=c(0.5,dim(tbl)[1]), type="n")
lapply(1:dim(tbl)[1], function(x) {
  text(x=c(1:dim(tbl)[2]), y=rep(x,dim(tbl)[2]), labels=tbl[x,])
})

Using this as a basis, you can play around with textand graphics argsto style the schedule as you wish.

+4
source

Here are two possible solutions based on fake data generated using this helper function:

generate.data <- function(rate=.3, dim=c(25,25)) {
  tmp <- rep(".", prod(dim))
  tmp[sample(1:prod(dim), ceiling(prod(dim)*rate))] <- "S"
  m <- matrix(tmp, nr=dim[1], nc=dim[2])
  return(m)
}
  • Text output

    x <- generate.data()
    rownames(x) <- colnames(x) <- 1:25
    capture.output(as.table(x), file="res.txt")
    

    The file res.txtincludes a fairly printed version of the console output; you can convert it to pdf using any txt to pdf converter (I use it from PDFlib). Here is a screenshot of the text file:

    enter image description here

  • Image Based Output

    -, :

    make.table <- function(x, labels=NULL) {
      # x = matrix
      # labels = list of labels for x and y
      coord.xy <- expand.grid(x=1:nrow(x), y=1:ncol(x))
      opar <- par(mar=rep(1,4), las=1)
      plot.new() 
      plot.window(xlim=c(0, ncol(x)), ylim=c(0, nrow(x)))
      text(coord.xy$x, coord.xy$y, c(x), adj=c(0,1)) 
      if (!is.null(labels)) {
        mtext(labels[[1]], side=3, line=-1, at=seq(1, ncol(x)), cex=.8)
        mtext(labels[[2]], side=2, line=-1, at=seq(1, nrow(x)), cex=.8, padj=1)
      }
      par(opar)
    }
    

    make.table(x, list(1:25, 1:25))
    

    ( png, pdf, jpg - ).

    enter image description here

+3

As far as I can see, this is a text table. I wonder why you want to make it a schedule? Anyway, quick fixes (anyway)

+2
source

All Articles