[R] Some problems with back to back bar plots in ggplot2

S Ellison S.Ellison at lgcgroup.com
Wed Jul 17 16:53:23 CEST 2013


> Hi, I'm making a simple population pyramid using two 
> back-to-back bar plots for 18 different age groups with 
> totals for males and females.

I can run both variants without error, though both throw the same warning regarding stacking beein 'not well defined ..'

However, the axis scaling in your second variant appears to work if you add a limits=(c(-200,200) statement to the y scale, as in

ggplot(data=df,aes(x=df$ag))+geom_bar(subset=.(df$variable=="males"),aes(y=df$value),stat="identity",fill="#330099")+
  geom_bar(subset=.(df$variable=="females"),aes(y=-df$value),stat="identity",fill="#FF9333")+
  scale_y_continuous(limits=c(-200,200), breaks=seq(-200,200,50),labels=abs(seq(-200,200,50)))+
  scale_x_continuous(breaks=seq(1,18,1),labels=abs(seq(1,18,1)))+coord_flip()+
  theme_bw()+xlab("age group")+ylab("population")


I suspect that by specifying the bar values separately from the data supplied in the data statement, you've confused ggplot's automatic scaling somewhere. But you may have to wait on hadley Wickam's input to find out exactly why.

S Ellison



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list