R ggplot加权条形图

时间:2016-10-24 07:20:18

标签: r ggplot2

我有一个数据框,我从中生成一个条形图,我使用的两个列是:

TravelMode: Car/Van Car/Van Bus Train Bus Train Bicycle Car/Van Walk....
GovtRegion: North West South West London Scotland North West North West North East....

这些中的每一个都是分别具有7和9级的因子。如果我生成了这些的tally(),我可以添加一个权重属性,以便我的输出百分比被“纠正”:

tally(TravelMode ~ GovtRegion, data=qlfs_clean, margin='col', format='percent', weights='Weights', na.rm=TRUE)

但是我似乎无法使用ggplot堆叠条来处理这个权重。有什么方法可以添加它,以便我的条形图中的百分比是加权百分比而不是原始百分比?

如果我在ggplot的aes()属性中使用weight =“Weights”,则返回一个空图表:

ggplot(data=na.omit(qlfs_clean)) + geom_bar(aes(x=GovtRegion, fill=TravelMode, weight="Weights"),position="fill") + scale_y_continuous(labels = percent_format()) + ylab("") + xlab("Region") + facet_wrap(~AgeCat) + scale_fill_discrete(breaks=c("Car/Van","Motorcycle","Bicycle","Bus","Local Metro/Tram/Train","Walk","Other method")) + scale_fill_manual(values=cbPalette) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

0 个答案:

没有答案