
Question:
I have a data of 100 rolls of two dice, which can take on 11 values -> {2,3,4,5,6,7,8,9,10,11,12}
How do I create a histogram in R that would show all 11 of them, each as it's own bar with a label for each one of them.
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")
Only gives 10 bars.
EDIT:
I did something approximate with moving breaks 0.5 up like so:
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)
Answer1:Histograms can also be plotted with ggplot. Using @flodel's data:
dd = data.frame(table(sums))
ggplot(dd)+geom_bar(aes(x=sums, y=Freq), stat='identity')
<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/xtx9n.png" data-original="https://i.stack.imgur.com/xtx9n.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />
Answer2:You could just do a barplot of the frequency table:
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))
<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/AHWgm.png" data-original="https://i.stack.imgur.com/AHWgm.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />