无法处理频率直方图中的箱数

时间:2019-02-27 22:36:55

标签: r histogram frequency

我需要对数据进行频率直方图处理。应该很简单。但是,我似乎无法操纵垃圾箱的大小,即中断的次数!

以下是一些示例数据:

dput(testdata)
structure(list(Year = c(1970, 1971, 1972, 1973, 1974, 1975, 1976, 
1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 
1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 2016, 1970, 1971, 1972, 1973, 
1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 
1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 1970, 
1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 
1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 
1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 
1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 
1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
2012, 2013, 2014, 2015, 2016), ratio = c(0.857142857142857, 1.25806451612903, 
1.96296296296296, 1.14705882352941, 1.70833333333333, 1.125, 
0.944444444444444, 1, 1.45161290322581, 1.92, 1.45454545454545, 
1.76470588235294, 1.02173913043478, 0.755555555555556, 0.8, 0.875, 
1.55555555555556, 1.5, 0.96, 2.28571428571429, 1.38095238095238, 
1.875, 1.66666666666667, 1.75, 1.80952380952381, 2, 2.05555555555556, 
1.41666666666667, 1.65384615384615, 1.82352941176471, 1.41666666666667, 
2.75, 2.05357142857143, 2.03409090909091, 2.02564102564103, 1.85227272727273, 
2.05263157894737, 1.69354838709677, 1.22972972972973, 1.81666666666667, 
2.27659574468085, 1.94545454545455, 1.82456140350877, 1.57692307692308, 
1.74, 1.58823529411765, 1.52272727272727, 2.41666666666667, 1.52941176470588, 
1.5, 2.5, 1.11627906976744, 1.5, 1.29545454545455, 1.03174603174603, 
1.37777777777778, 1.16666666666667, 1.6530612244898, 1.70833333333333, 
1.27272727272727, 2.09302325581395, 1.40350877192982, 1.66666666666667, 
1.30909090909091, 1.33333333333333, 1.6078431372549, 1.71153846153846, 
1.66666666666667, 1.52, 1.64705882352941, 1.46428571428571, 1, 
2.24324324324324, 1.77142857142857, 2.0625, 0.973684210526316, 
3.76470588235294, 1.72972972972973, 2.62962962962963, 1.75, 1.66666666666667, 
2.84, 2.92, 1.5, 1.42857142857143, 1.97222222222222, 2.51612903225806, 
1.725, 1.675, 1.73170731707317, 1.05769230769231, 1.44736842105263, 
1.45652173913043, 2.53333333333333, 1.89655172413793, 1.76, 0.916666666666667, 
2.03846153846154, 1.5625, 1.37931034482759, 2.12, 1.91666666666667, 
2.57142857142857, 2.65384615384615, 2.25925925925926, 1.4, 2, 
1.67741935483871, 2.6, 2.07407407407407, 1.875, 3.0952380952381, 
4.71428571428571, 2.52173913043478, 4.57142857142857, 3.47368421052632, 
3.10526315789474, 3.26315789473684, 2.77272727272727, 2.76190476190476, 
2.85, 2.9047619047619, 3.05, 3.625, 2.94736842105263, 5.2, 2.94444444444444, 
3.73333333333333, 2.59090909090909, 3.35, 3.5, 3.7, 2.89473684210526, 
1.61111111111111, 1.7037037037037, 3.2, 2.26666666666667, 2.03333333333333, 
2.03571428571429, 3, 2.79166666666667, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.6875, 
3.25, 1.43478260869565, NA, NA, NA, NA, 2.8, 2.05263157894737, 
4.30769230769231, 2.77777777777778)), row.names = c(NA, -188L
), class = "data.frame")

我使用grid :: histogram,我的脚本如下:

library(lattice)
bins <- seq(0,10,0.25)
histogram(testdata$ratio,type="count",xlab="Ratio")

此脚本有效,但是无论我执行的bin大小是否等于1,我都需要为0.5。

2 个答案:

答案 0 :(得分:2)

只需将breaks的{​​{1}}参数设置为lattice::histogram()

bins

答案 1 :(得分:1)

不直接回答问题,但您也可以使用ggplot2

library(tidyverse)

plot <- ggplot(data = testdata, aes(ratio))
plot + geom_histogram(binwidth = 0.25)