开发者

Graphic of binary variable in R

开发者 https://www.devze.com 2023-03-10 02:47 出处:网络
I would like to plot a simple graphic. I have a dat set with n rowns and k columns, in which each row has a a sequence of 0 and 1. I would like to plot exactly this sequence for all rows.

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

Actually I want to reproduce the figure 24.1, p. 516, of Gelman and Hill's book (Data aAnalysis Using Regression and Multilevel/Hierarchical Models). I suspect that he made the graphic in Latex, but it seems quite ridiculous that I'm not able to repplicate this simple graphic开发者_Go百科 in R. The figue is something like this. As you can see from the link, the "ones" are replaced by "S" and "zeros" by ".". It's a simple graphic, but it shows each individual response by time.


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

Given matrix tbl containing your data:

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

You can generate a plot 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 base you can play around with the text and plot args to stylize the plot the way you wish.


Here are two possible solutions, based on fake data generated with 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)
}
  1. Text-based output

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

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

    Graphic of binary variable in R

  2. Image-based output

    First, here is the plotting function I used:

    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)
    }
    

    Then I call it as

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

    and here is the result (save it as png, pdf, jpg, or whatever).

    Graphic of binary variable in R


As far as I can see, this is a text table. I am wondering why you want to make it a graph? Anyway, quick solutions are (either way)

  1. make the text table (by programming or typing) and make its screenshot and embed the image into the plot.

  2. make a blank plot and put the text on the plot by programming R with "text" function. For more info on "text", refer to http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_adtxt.html

0

精彩评论

暂无评论...
验证码 换一张
取 消