2015-10-07 164 views
1

我有三列的数据帧:显示频率(%)和计数

  1. 结果:因子变量(以两行)
  2. n:整数变量显示的时间数因子变量出现在数据帧
  3. FREQ:DBL可变显示所述因子变量的数据的频率设定

    DF < - data.frame(结果= as.factor(C(“良好”, “Bad”)), N = C(700,300),FREQ = C(70,30))

我使用下面的代码基于我因子可变的频率下产生一个条形图:

library(ggplot2) 

ggplot(df, aes(x=outcome, y=freq, fill=outcome)) + 
    geom_bar(stat="identity", width=.4) + 
    geom_text(aes(label=paste0(freq,"%")), vjust=1.5, colour="white") 

此代码显示频率上方各条,这样70% enter image description here

我想显示的频率和对我的条形图的顶部计数的东西。例如:70% (4532) 如果可能,使用百分比和计数之间的换行符。

有关如何实现这一点的任何想法?

+2

【如何使一个伟大的[R重复的例子?(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ) – zx8754

+1

如何将'label'改为'paste0(freq,“%\ n(”,n,“)”) – Axeman

回答

1
ggplot(df, aes(x=outcome, y=freq, fill=outcome)) + 
geom_bar(stat="identity", width=.4) + 
geom_text(aes(label=paste0(freq, "%\n(", n, ")"), vjust=1.5, colour="white") 

enter image description here

+0

只是一个问题,为什么你必须编写“%\ n(”,n,“) “而不仅仅是”%\ n“,ñ – remif

+1

@remif所以你可以有括号? - '70%(4532)' – zx8754

+0

明白了,谢谢! (并为我的愚蠢问题感到抱歉) – remif