2015-10-07 161 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