修改箱标签直方图 [英] Modify bin labels histogram

查看:92
本文介绍了修改箱标签直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下直方图:

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-3;而不是x轴上的2-8. 4-5; 6-8

Instead of a range from 2-8 on the x-axis, I need three labels on the x-axis that summarize the range of values like this: 2-3; 4-5; 6-8

如何修改x轴的代码,使其仅在正确的位置得到三个标签?

How can I modify the code for the x-axis to get just three labels, and in the correct location?

推荐答案

使用以下方法很容易为范围的中点标记:

It's easy to label midpoints for the ranges using:

h <- hist(one, breaks = 3, xaxt = 'n')
axis(1, h$mids, h$mids)

但是,如果要使标签成为命名范围的字符串,则必须做更多的工作.看看str(h)可以使用的内容:

But if you want to have the labels be character strings naming the ranges, you have to do a little more work. Take a look at str(h) to see what you have to work with:

> 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元素构造轴标签:

You can use the breaks element to construct axis labels:

h <- hist(one, breaks = 3, xaxt = 'n')
axis(1, h$mids, paste(h$breaks[1:3], h$breaks[2:4], sep=' - '))

这篇关于修改箱标签直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆