2014-10-02 101 views
0

我有兩個骰子100卷的數據,它可以取11個值 - > {2,3,4,5,6,7,8,9,10, 11,12}每個頻率值帶有一個條的直方圖

如何在R中創建一個直方圖來顯示它們中的全部11個,每個直方圖都是它自己的酒吧,每個酒吧都有一個標籤。

hist(data$X1,breaks=c(1,2,3,4,5,6,7,8,9,10,11,12,13),col = "lightblue",xlab="Sum of a roll") 

只給出10個小節。

編輯:

我做了大致與移動減免0.5像這樣:

breaks=c(1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5) 
+0

還有呢?什麼讓你的新休息?它回答你的問題嗎? – 2014-10-02 03:19:39

+0

直方圖用於連續隨機變量(並且您的示例是離散的)。像這樣的'barplot()'可能更合適。 – MrFlick 2014-10-02 04:28:01

回答

0

直方圖也可以用ggplot繪製。使用@ flodel的數據:

dd = data.frame(table(sums)) 
ggplot(dd)+geom_bar(aes(x=sums, y=Freq), stat='identity') 

enter image description here

3

你可能只是這樣做的頻率表的barplot:

num.dices <- 2L 
num.rolls <- 100000L 
outcomes <- matrix(sample(1:6, num.dices * num.rolls, replace = TRUE), 
        nrow = num.rolls, ncol = num.dices) 
sums <- rowSums(outcomes) 
barplot(table(sums)) 

enter image description here