【问题标题】:Create a function to change bin size in a histogram with hist in R创建一个函数以使用 R 中的 hist 更改直方图中的 bin 大小
【发布时间】:2017-07-28 02:59:49
【问题描述】:

编写一个函数,该函数接受一个向量、一个整数向量、一个主轴标签和一个 x 轴标签。此函数应遍历整数向量中的每个元素并为每个整数值生成直方图,将 bin 计数设置为输入向量中的元素,并使用指定参数标记主轴和 x 轴。您应该标记 y 轴以读取频率、箱 = 和箱数。

我正在尝试这个,但它没有在 nclass 中正确读取三种不同的 bin 大小。我做错了什么?

plot.historgrams <- function(x,nclass, ...){
  for (i in 1:length(nclass)) {
  hist(x,nclass=nclass[i], ...)
  }
}

plot.histograms(hidalgo.dat[,1], c(12,36,60), main="1872 Hidalgo 
   issue",xlab= "Thickness (mm)")

【问题讨论】:

    标签: r function for-loop histogram


    【解决方案1】:

    当我使用中断并将 x= 放在数据集前面时,它似乎工作正常。

    plot.historgrams <- function(x,b, ...){
      for (i in 1:length(b)){
      hist(x=x,breaks=b[i], ylab=paste("Frequency, bins= ",b[i]),  ...)
      }
    }
    
    plot.historgrams(x=hidalgo.dat[,1], c(12,36,60), main="1872 Hidalgo issue",xlab= "Thickness (mm)")
    

    【讨论】:

    • 有谁知道我为什么要把 x = 放在那里?
    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 2022-01-10
    • 2017-07-05
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多