2016-06-21 3 views
1

인구 피라미드를 만들고 있습니다. 인구수에 음수가 없도록 축에 절대 값을 사용하고 싶습니다. 또한 쉼표로 포맷 된 x 축에 눈금을 표시하여 30000 대신 30,000을 읽도록하고 싶습니다.ggplot 레이블에 labels = abs 및 labels = comma를 어떻게 사용합니까?

나는 labels=comma이 쉼표를 사용하는 숫자를 제공 할 것이고, 나는 labels =abs이 절대 값을 제공한다는 것을 알고 있습니다. 두 옵션을 어떻게 결합합니까?

#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)) 

나는 절대 값을 얻을 수있는 원래의 상단에 다른 규모와 같은 추가 시도했지만 작동하지 않았다. 여기 내 시도입니다 :

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