I used the gjabel answer to create the population pyramid for my data.
My data is similar to the example below where for certain ages there is no representation of that age in either the female or male sex.
#individual level data
Age<-c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,3,5,10,30,90)
Sex<- c("Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Male","Female", "Female","Female", "Female","Female")
test <- data.frame(Age, Sex)
The final result should show a continuous y-axis a sequence from 0 to 90 with breaks by 1. If there's no one with that age then there would be no bar but a space where the age category would be.
- How would I go about representing those ages as zero count in a population pyramid?
How do I make both the male and female side symmetrical on the x axis? I would like both sides to have the same x limit for symmetry in my plot.
require(ggplot2) require(plyr) ggplot(data=test,aes(x=as.factor(round(Age)),fill=Sex)) + geom_bar(data= subset(test,test$Sex=="Female")) + geom_bar(data= subset(test, test$Sex=="Male"), mapping=aes(y=..count..*(-1)), position="identity") + scale_y_continuous(breaks=seq(-50,50,10),labels=abs(seq(-50,50,10))) + xlab("Age (years)")+ ylab("Count") + scale_x_discrete(breaks = c(0,10,20,30,40,50,60,70,80,90))+ coord_flip()