2016-06-21 3 views
1

私は人口ピラミッドを作成しています。私は絶対値を使用するように私の軸をしたいので、私は人口の数に負の数を持っていません。また、30000の代わりに30,000を読み込むように、カンマでフォーマットされたx軸に目盛りを付けることもできます。どのようにggplotラベルにlabels = absとlabels = commaの両方を使用させるのですか?

私はlabels=commaがコンマを使用する数字を提供することを知っています。私はlabels =absが絶対値の数字を提供することを知っています。 2つのオプションを組み合わせるにはどうすればよいですか?

#test data 
    mfrawcensus <- data.frame(Age = factor(rep(x = 1:90, times = 2)), 
       Sex = rep(x = c("Females", "Males"), each = 90), 
       Population = sample(x = 1:60000, size = 180)) 

censuspop <- ggplot(data=mfrawcensus,aes(x=Age,y=Population, fill=Sex)) + 
geom_bar(data= subset(mfrawcensus,Sex=="Females"), stat="identity") + 
geom_bar(data= subset(mfrawcensus,Sex=="Males"), 
     mapping=aes(y=-Population), 
     stat="identity", 
     position="identity") + 
scale_y_continuous(labels=comma) + 
xlab("Age (years)")+ ylab("Population") + 
scale_x_discrete(breaks =seq(0,90,5), drop=FALSE)+ coord_flip(ylim=c(-55000,55000)) 

私は絶対値を取得するには、元の1の上に別のスケールのように追加してみましたが、それはうまくいきませんでした。ここに私の試みです:

censuspyramid<-censuspop+theme_bw() +theme(legend.position="none") 
    + ggtitle("a")+scale_y_continuous(labels=abs) 

答えて

3

あなたはこの代わりの両方abscomma

abs_comma <- function (x, ...) { 
    format(abs(x), ..., big.mark = ",", scientific = FALSE, trim = TRUE) 
} 

を使用しない新しい機能を作ることができcomma

関連する問題