2012-09-19 49 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