0

I've been working on some epidemiologic data using R and ggplot. I've plotted a population pyramid using the following code:

Data is available here, using multiplot script from here.

plot1 <- ggplot(pt.indig, aes(x=agecat, fill=gender)) +
   geom_bar(position="dodge") +
   scale_fill_brewer("Gender", palette="Set1") + 
   scale_y_continuous(breaks=c(0:10), minor_breaks=NULL) + coord_flip() +
   labs(y="Indigenous Patients", x=NULL) + coord_flip() +
   theme_bw() + theme(axis.text.y=element_text(hjust=-0.1))

plot2 <- ggplot(pt.nind, aes(x=agecat, fill=gender)) + 
   geom_bar(aes(y=..count..*(-1)), position="dodge") +
   scale_fill_brewer(palette="Set1") + coord_flip() + 
   scale_y_continuous(breaks=seq(-11,0,1), labels=abs(seq(-11,0,1))) +
   labs(x=NULL, y="Non-Indigenous Patients") + 
   theme(legend.position="none", axis.text.y=element_blank(), axis.ticks.y=element_blank())

multiplot(plot2, plot1, cols=2)

Produces this graph: poppyr

What I'd like to do with the graph is:

  1. Make the two plots the same relative size
    • horizontally so the bars are proportionate
    • vertically so the age categories line up properly
  2. Move the left graph a bit closer to the y-axis labels (or the labels across, but using hjust displaces the shorter labels (NA, and the <10 year old labels))
  3. Make the Non-indig 10-14 year-old male bar show as the same width as the others
  4. (ideally, make the NA bars the same colour, but less important than the others)

Help greatly appreciated.

Trent
  • 55
  • 1
  • 8
  • oops - obviously shouldn't have `coord_flip()` twice in `plot1` – Trent Oct 15 '13 at 23:59
  • Your examples aren't reproducible: downloading the csv and trying to run your plotting code, I got `object 'agecat' not found`, so I'm assuming you've added a few variables to the raw data. – Marius Oct 16 '13 at 00:04
  • I would do the following: 1) combine the datasets; 2) produce a facetted plot p1 with the left panel like you want; 3) same with facetted plot p2 for the right panel; 4) use gtable to combine the left and right panels plus y axis of p1 and p2. – baptiste Oct 16 '13 at 00:12
  • Yes, sorry - I obviously categorised the data into the age groupings on the plots. – Trent Oct 16 '13 at 00:13
  • 2
    see http://stackoverflow.com/questions/14680075/simpler-population-pyramid-in-ggplot2 or http://stackoverflow.com/questions/18265941/two-horizontal-bar-charts-with-shared-axis-in-ggplot2-similar-to-population-pyr – mnel Oct 16 '13 at 00:15
  • Have a look @ http://docs.ggplot2.org/0.9.3.1/theme.html as well and the gridExtra package can be very useful too. – Tyler Rinker Oct 16 '13 at 01:16

0 Answers0