我需要建立一个有序的水平条形图,它具有频率计数(我可以获得)以及它顶部(侧面)的频率百分比。到目前为止我的代码看起来像这样。我无法订购图表或添加百分比(除了计数)。任何帮助深表感谢。对不起,我是新来的,没有足够的声誉来发布示例图片。
event <- c("a","a","a","a","a","a","b","b","b","b","c","c","c","c","c")
dat <- data.frame(event)
ggplot(dat, aes(x=event)) + geom_bar(fill = "orange", width = 0.5) + coord_flip() +
geom_text(stat='bin',aes(label=..count..,vjust=.75, hjust = 0))
感谢您的帮助
答案 0 :(得分:0)
更改订单可以通过事先更改因素来完成:
dat$event <- factor(dat$event, levels = names(sort(table(dat$event)))).
添加百分比就像使用绝对值一样:
geom_text(stat='bin',
aes(label=paste0(..count..,
", ",
round(..count../sum(..count..)*100, 1),
"%")))