[R] turning the output of cut into a waffle plot

Jim Lemon drjimlemon at gmail.com
Wed Jan 18 00:21:31 CET 2017


Hi all,
A few days ago I offered a suggestion on how to display the initial
values that were cut into a factor as a waffle plot. As Rolf Turner
noted, a major problem for users would be constructing the matrix that
was fed to the color2D.matplot function. Here is a fairly general
purpose function for that with an example from the initial post.

egdat <- c(137,135,144,149,150,152,159,157,154,163,164,164,
           161,162,165,164,179,173,173,182,180,185,180,197,190)

cut2matrix<-function(x,breaks,ncol,nrow,right=TRUE) {
 xcut<-as.numeric(cut(x,breaks=breaks,right=right))
 if(missing(ncol)) ncol<-length(breaks)-1
 if(missing(nrow)) nrow<-max(table(xcut))
 xlist<-vector("list",ncol)
 for(xind in 1:ncol) xlist[[xind]]<-rev(x[xcut==xind])
 xdf<-as.data.frame(lapply(xlist,function(x) x[1:nrow]))
 names(xdf)<-paste("V",1:ncol,sep="")
 return(as.matrix(sapply(xdf,rev)))
}

egmat<-cut2matrix(egdat,seq(120,210,by=10),9,8,FALSE)
library(plotrix)
color2D.matplot(egmat,show.values=TRUE)

Jim



More information about the R-help mailing list