[R] Bumps chart in R
Jim Lemon
jim at bitwrit.com.au
Mon Apr 27 14:32:48 CEST 2009
Andreas Christoffersen wrote:
> Hi there,
>
> I would like to make a 'bumps chart' like the ones described e.g.
> here: http://junkcharts.typepad.com/junk_charts/bumps_chart/
>
> Purpose: I'd like to plot the proportion of people in select countries
> living for less then one USD pr day in 1994 and 2004 respectively. I
> have already constructed a barplot - but I think a bumps chart would
> be better
>
> # The barplot and data
> countries <- c("U-lande", "Afrika syd for sahara", "Europa og
> Centralasien", "Lantinamerika og Caribien","Mellemøstenog Nordafrika",
> "Sydasien","ØStasien og stillehaveet", "Kina", "Brasilien")
> poor_1990 <- c(28.7,46.7,0.5,10.2,2.3,43,29.8,33,14)
> poor_2004 <- c(18.1,41.1,0.9,8.6,1.5,30.8,9.1,9.9,7.5)
> poor <- cbind(poor_1990,poor_2004)
> rownames(poor) <- countries
> oldpar <- par(no.readonly=T)
> par <- par(mar=c(15,5,5,1))
> png("poor.png")
> par <- par(mar=c(15,5,5,1))
> barplot(t(poor[order(poor[,2]),]),beside=T,col=c(1,2),las=3,ylab="%
> poor",main="Percent living for < 1 USD per day (1993
> prices)",ylim=c(0,50))
> legend("topleft",c("1990","2004"),fill=c(1,2),bty="n")
> par(oldpar)
> dev.off()
>
> I Guess I need to start with an normal plot? Something like the below
> - but there is a loong way to go...
>
> # A meager start - how to finish my bumps chart
> plot(c(rep(1,9),rep(2,9)),c(fattig_1990,fattig_2004),type="b",ann=F)
>
>
Hi Andreas,
Not too hard. Try this:
bump.plot<-function (y1,y2=NULL,top.labels=NULL,left.labels=NULL,
right.labels=NULL,rank=TRUE,descending=TRUE,linecol=par("fg"),
mar=c(2,2,4,2),...) {
if(missing(y1))
stop("Usage: spread.labels(y1,y2,labels,...)")
ydim<-dim(y1)
if(is.null(ydim) && is.null(y2))
stop("y1 must be a matrix or data frame if y2 is NULL")
oldmar<-par("mar")
par(mar=mar)
if(is.null(y2) && ydim[2] > 1) {
y2<-y1[,2]
y1<-y1[,1]
}
if(rank) {
left.labels<-left.labels[order(y1)]
right.labels<-right.labels[order(y1)]
y1<-rank(y1)
y2<-rank(y2)
if(descending) {
left.labels<-rev(left.labels)
right.labels<-rev(right.labels)
y1<-rev(y1)
y2<-rev(y2)
}
}
ny<-length(y1)
plot(c(rep(1,ny),rep(2,ny)),c(rev(y1),rev(y2)),xlim=c(0,3),xlab="",ylab="",
axes=FALSE,...)
segments(rep(1,ny),y1,rep(2,ny),y2,col=linecol)
text(0.8,rev(y1),left.labels,adj=1)
text(2.2,rev(y2),right.labels,adj=0)
par(mar=oldmar)
}
# use the above data
bump.plot(poor_1990,poor_2004,c("1990","2004"),countries,countries,
linecol=1:20,pch=16,main="Bumps plot")
Jim
More information about the R-help
mailing list