A histogram with a bar for each frequency value

风格不统一 提交于 2019-12-14 03:08:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!