【问题标题】:A histogram with a bar for each frequency value每个频率值的直方图
【发布时间】:2014-10-02 03:04:37
【问题描述】:

我有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)

【问题讨论】:

  • 然后呢?是什么让你有新的突破?它回答了你的问题吗?
  • 直方图用于连续随机变量(您的示例是离散的)。因此,barplot() 可能更合适。

标签: r histogram


【解决方案1】:

您可以只绘制频率表的条形图:

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))

【讨论】:

    【解决方案2】:

    直方图也可以用 ggplot 绘制。使用@flodel 的数据:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-11
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多