[R] "shifted" bar chart / battleship curve
Jim Lemon
jim at bitwrit.com.au
Thu Feb 2 11:36:03 CET 2012
On 02/02/2012 01:24 PM, Maltese, Adam Vincent wrote:
> What I want to do is create a horizontal bar chart matrix for a set of data that have a common set of variables (e.g., % of As, % of Bs, etc.) listed on the Y-axis and groups (e.g., Classes) on the X-axis. The key here is that the bars for each individual class plot are "centered" rather than left or right-justified. In archaeology plots similar to this are called battleship curves or Ford Diagrams, but the suggestions I've received involved manual centering of the bars in Adobe Illustrator. Any suggestions for automating this process are much appreciated. An example of what this might look like can be seen here: http://tfqa.com/graphics/ford2.gif
>
Hi Adam,
It's fairly easy to get the attached plot with the following function.
I'll add it to the next version of plotrix (out shortly).
Jim
battleship.plot<-function(x,mar=c(2,5,5,1),col="white",
border="black",main="",xlab="",ylab="",xaxlab=NA,yaxlab=NA,
maxxspan=0.45,maxyspan=0.45) {
dimx<-dim(x)
if(length(dimx) != 2)
stop("battleship.plot(x) where x is a 2 dimensional matrix or data
frame")
par(mar=mar)
plot(0,xlim=c(0.5,dimx[2]+0.5),ylim=c(0.5,dimx[1]+0.5),
axes=FALSE,type="n",main=main,xlab="",ylab="")
mtext(xlab,side=1,line=0)
if(is.na(xaxlab[1])) {
xaxlab<-colnames(x)
if(is.null(xaxlab)) xaxlab<-1:dimx[2]
}
staxlab(side=3,at=1:dimx[2],labels=xaxlab,srt=45,
adj=0,top.line=0,ticklen=0)
if(is.na(yaxlab[1])) {
yaxlab<-rownames(x)
if(is.null(yaxlab)) yaxlab<-1:dimx[1]
}
staxlab(side=2,at=dimx[1]:1,labels=yaxlab,nlines=1,srt=0,adj=1,
top.line=0,ticklen=0)
normx<-maxxspan*x/max(x,na.rm=TRUE)
rect(rep(1:dimx[2],each=dimx[1])-normx,rep(dimx[1]:1,dimx[2])-maxyspan,
rep(1:dimx[2],each=dimx[1])+normx,rep(dimx[1]:1,dimx[2])+maxyspan,
col=col,border=border)
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: battleship1.pdf
Type: application/pdf
Size: 5324 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120202/dd88cb97/attachment.pdf>
More information about the R-help
mailing list