问题
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)
回答1:
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')

回答2:
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))

来源:https://stackoverflow.com/questions/26153925/a-histogram-with-a-bar-for-each-frequency-value