2012-09-19 38 views
0

我有一個輪盤模擬,繪製頻率與輪盤輪槽(或因子)的圖,但我也想查看%相對頻率與因子。我如何顯示%相對頻率而不是頻率

black_on_wheel = paste("B", 1:18, sep = "") 
red_on_wheel = paste("R", 1:18, sep = "") 
roulette_wheel = c(red_on_wheel, black_on_wheel, "0", "00") 
simulated_roulette_wheel = sample(roulette_wheel, size=500, replace = TRUE) 
plot(rw_runs) 
+1

你的代碼似乎不完整的。什麼是'rw_runs'?你甚至考慮過'table'和'prop.table'嗎? – joran

回答

0

由於@joran指出的那樣,你可以使用tableprop.table

set.seed(001) # For the simulation to be reproducible. 
simulated_roulette_wheel = sample(roulette_wheel, size=500, replace = TRUE) 

tab <-table(simulated_roulette_wheel)     # Frequency of each factor 
prop.tab <- prop.table(tab) * 100      # % Relative Freq. 
barplot(prop.tab, xaxs='i', ylab="Relative %") ; box() # Barplot 

barplotxaxs="i"允許酒吧開始在X座標的原點和功能box()增加一個盒子的情節。

prop.tab前十個元素的樣子:

prop.tab[1:10] 
simulated_roulette_wheel 
    0 00 B1 B10 B11 B12 B13 B14 B15 B16 
2.4 2.8 4.6 3.4 2.2 3.0 1.8 2.4 2.2 2.2 

如果不乘以100 prop.table(tab),那麼你將只能獲得一定比例的,而不是相對百分比。

這裏產生的barplot:

enter image description here

0
rw_runs <- table(simulated_roulette_wheel) 
str(rw_runs) 
# 'table' int [1:38(1d)] 19 9 13 8 19 16 12 11 14 13 ... 
# - attr(*, "dimnames")=List of 1 
# ..$ simulated_roulette_wheel: chr [1:38] "0" "00" "B1" "B10" ... 
barplot(rw_runs*100/sum(rw_runs)) 
+0

他們都很好。 – oaxacamatt