# How to make age pyramid like plot in R?

Age pyramid looks like this:
I would like to make something similar, namely a 2 barplots (not histograms) with same categories, rotated vertically and extending to both sides as in pyramid.
Is it a simple way to do this in R?
It would be also nice to control the colour of each bar.

## locked by whuber♦Jul 17 '16 at 19:59

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

Read more about locked posts here.

## 1 Answer

You can do this with the pyramid.plot() function from the plotrix package. Here's an example:

library(plotrix)
xy.pop<-c(3.2,3.5,3.6,3.6,3.5,3.5,3.9,3.7,3.9,3.5,3.2,2.8,2.2,1.8,
1.5,1.3,0.7,0.4)
xx.pop<-c(3.2,3.4,3.5,3.5,3.5,3.7,4,3.8,3.9,3.6,3.2,2.5,2,1.7,1.5,
1.3,1,0.8)
agelabels<-c("0-4","5-9","10-14","15-19","20-24","25-29","30-34",
"35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74",
"75-79","80-44","85+")
mcol<-color.gradient(c(0,0,0.5,1),c(0,0,0.5,1),c(1,1,0.5,1),18)
fcol<-color.gradient(c(1,1,0.5,1),c(0.5,0.5,0.5,1),c(0.5,0.5,0.5,1),18)
par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels,
main="Australian population pyramid 2002",lxcol=mcol,rxcol=fcol,
gap=0.5,show.values=TRUE))


Which ends up looking like this:

• Ok, indeed I rewrote it completely to have different scales on both sides, but it seems there is nothing in R that can do it natively. – user88 Sep 9 '10 at 13:24
• I'm sure that you could do it with ggplot2, but it would require a little effort. – Shane Sep 9 '10 at 13:44
• @mbq Also FYI there is a pyramid chart capabilities in the HH package. This question on the site gives examples for Likert type data, which I imagine can be hacked to produce similar bar charts. In ggplot2 one would need to display reflected counts, an easy way is to just have one bar be negative and the other be positive (not sure if ggplot2 supports mirrored dimensions like pyramid plots though). – Andy W Feb 3 '13 at 14:45