R:具有自定义中断和恒定宽度的直方图

时间:2015-07-14 18:35:29

标签: r ggplot2 histogram

我有一些偏斜的数据,并且想要创建一个带有自定义中断的直方图,但是希望它实际上看起来是可读的,具有恒定宽度的二进制位(这会甩掉x轴的比例,但是那个' s精细)。有没有人知道如何在ggplot / R中做到这一点?

这是我不想要的,但我不知道如何让休息不会覆盖宽度参数:

library(ggplot2)
test_data = rep(c(1,2,3,4,5,8,9,14,20,42,98,101,175), c(50,40,30,20,10,6,6,7,9,5,6,4,1))
buckets = c(-.5,.5,1.5,2.5,3.5,4.5,5.5,10.5,99.5,200)
q1 = qplot(test_data,geom="histogram",breaks=buckets)
print(q1)

不是我想要的直方图:(

Histogram with variable width bars

1 个答案:

答案 0 :(得分:2)

正如ulfelder建议的那样,使用cut()

library(ggplot2)
test_data = rep(c(1,2,3,4,5,8,9,14,20,42,98,101,175),
                c(50,40,30,20,10,6,6,7,9,5,6,4,1))
buckets = c(-.5,.5,1.5,2.5,3.5,4.5,5.5,10.5,99.5,200)
q1 = qplot(cut(test_data, buckets), geom="histogram")
print(q1)

Barplot showing histogram based on user defined "buckets"