[R] [FORGED] Re: Generating a Special Histogram

Jim Lemon drjimlemon at gmail.com
Thu Jan 5 23:39:24 CET 2017


A worthy challenge, Rolf:

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)
egcut<-cut(egdat,breaks=seq(120,210,by=10),right=FALSE)
eglist<-vector("list",9)
for(egindex in 1:9) eglist[[egindex]]<-rev(egdat[as.numeric(egcut)==egindex])
egdf<-as.data.frame(lapply(eglist,function(x) x[1:8]))
names(egdf)<-paste("V",1:9,sep="")
waffle.mat<-as.matrix(sapply(egdf,rev))

Jim


On Fri, Jan 6, 2017 at 9:21 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
> On 06/01/17 10:31, Jim Lemon wrote:
>>
>> Hi Dan,
>> This may help if your data is in the format below:
>>
>> waffle.mat<-matrix(c(rep(NA,14),137,135,rep(NA,6),144,149,
>>  rep(NA,3),150,152,159,157,154,
>>  NA,163,164,164,161,162,165,164,rep(NA,5),179,173,173,
>>  rep(NA,4),182,180,185,180,
>>  rep(NA,6),197,190,rep(NA,8)),ncol=9)
>> waffle.col<-matrix("lightblue",ncol=9,nrow=8)
>> waffle.col[is.na(waffle.mat)]<-NA
>> waffle.border<-matrix("blue",ncol=9,nrow=8)
>> waffle.border[is.na(waffle.mat)]<-NA
>> library(plotrix)
>> # use a waffle plot
>> color2D.matplot(waffle.mat,cellcolors=waffle.col,border=waffle.border,
>>  show.values=TRUE,xat=10,yat=10,xlab="",ylab="")
>> axis(1,at=1:8,labels=seq(130,200,by=10))
>> axis(2,at=1:8)
>> axis.break(1)
>
>
> Being picky-picky-picky I would like to point out that Duncan's and David's
> functions don't *quite* reproduce the picture in the pdf file
> that the OP attached, when called with the data from that picture:
>
> 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)
> myhist(egdat)
> boxhist(egdat)
>
> It's a matter of including the left or right endpoints in the bins.
>
> Duncan's function needs to swap "<" and "<=" in the definition of "keep"
> (and make a corresponding adjustment in the "|" clause, so as to look at
> the last rather than the first break value).
>
> David's function needs to set "right=FALSE" in the call to cut().
>
> Jim's waffle plot gets it right, at the expense of needing to have the
> data organised in an inconvenient form.
>
> All that being said, all of you blokes came up with solutions that are far
> beyond my capability of producing.  Hat's off to you.
>
> cheers,
>
> Rolf
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list