[R] Christmas tree graphs

Roger Bivand Roger.Bivand at nhh.no
Tue Aug 28 09:29:34 CEST 2001


On Tue, 28 Aug 2001, Andrew C. Ward wrote:

> 
> Hello R users,
> 
> I wish to draw a type of horizontal bar chart for two groups (males and 
> females). The values for one group will start at the y-axis and point to the 
> left, and the values for the other group will start at the y-axis and point to 
> the right. If you're (un)lucky, the resulting graph can assume the shape of a 
> Christmas tree.
> 
> The data are
> 	left.side <- c(107092, 113053, 121163, 112209, 106543, 72895, 46920, 
> 32606, 11106, 1134, 60, 5247)
> 	right.side <- c(102129, 109261, 123291, 117322, 110397, 71027, 50319, 
> 43805, 21074, 3621, 204, 6608)
> 
> The R function barchart allows me to stack bars and to juxtapose them, but I 
> can't see how to draw the bars outwards from a centreline. If anyone had some 
> hints on how to do this, I would be very grateful. Thank you!
> 

I've used this function to draw population pyramids - it's not polished
though, and is rather old:

pyramid <- function(kpop, mpop, title="")
{
	mk <- max(kpop)
	mm <- max(mpop)
	w <- 2.2 * max(c(mk, mm))
	m <- w / 2
	h <- w
	slice <- h / length(kpop)
	op <- par(xaxt="n", yaxt="n", pty="s")
	plot(c(0,w), c(0,h), type="n", main=title, xlab="", ylab="")
	segments(0,0,w,0)
	segments(m,0,m,h)
	for (i in 1:length(kpop)) {
		rect(m-mpop[i], slice*(i-1), m, slice*i, col="gray")
		rect(m, slice*(i-1), m+kpop[i], slice*i, col="gray")
	}
	par(op)
	par(yaxt="s")
}

It ought to annotate the x-axis too.

Roger

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no
and: Department of Geography and Regional Development, University of
Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list