我做了以下直方图:
one <- c(2,2,3,3,3,3,4,4,4,4,4,4,5,5,5,5,6,6,7,8)
hist(one, breaks = 3)

我需要的不是x轴上的2-8范围,而是x轴上的三个标签,它们概括了这样的值范围: 2-3;4-5;6-8。
我如何修改x轴的代码,使之只得到三个标签,以及正确的位置?
发布于 2014-07-30 12:56:35
使用以下方法很容易标记范围的中点:
h <- hist(one, breaks = 3, xaxt = 'n')
axis(1, h$mids, h$mids)但是,如果您想让标签是字符串命名范围,您必须做更多的工作。看一看str(h),看看您必须使用什么:
> str(h)
List of 6
 $ breaks  : num [1:4] 2 4 6 8
 $ counts  : int [1:3] 12 6 2
 $ density : num [1:3] 0.3 0.15 0.05
 $ mids    : num [1:3] 3 5 7
 $ xname   : chr "one"
 $ equidist: logi TRUE
 - attr(*, "class")= chr "histogram"您可以使用breaks元素构造轴标签:
h <- hist(one, breaks = 3, xaxt = 'n')
axis(1, h$mids, paste(h$breaks[1:3], h$breaks[2:4], sep=' - '))

https://stackoverflow.com/questions/25037210
复制相似问题