1
我正在創建一個人口金字塔。我希望我的軸使用絕對值,以便我沒有負數的人口數。我還希望在x軸上用逗號格式化刻度線,以便不用30000就能讀取30,000。如何讓ggplot標籤同時使用labels = abs和labels =逗號?
我知道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)