Histogram in R - x-axis not centered properly

二次信任 提交于 2019-12-02 00:09:43

问题


I have a histogram from a list d of values that I make by simply typing

hist(d)

And this is what I get:

How can I make it such that the x-axis extends all the way left to the origin of this plot (the bottom left corner)? Why does it cut off at -0.4?


回答1:


Macro's answer is by far the simplest route. However, if you really are unhappy with with the default behavior of hist (really, it's the default behavior of axis I suppose) you can always suppress the axes and draw them yourself:

set.seed(123)
d <- rnorm(1000)
hist(d,axes = FALSE)
axis(1,at = seq(-3,3,1),labels = TRUE,pos = 0)
axis(2,pos = -3)

As for the "why?", the defaults for drawing axes have to be set at something, and so there's a lot of code under there that tries pretty hard to ensure that the axis and tick labels are "pretty" according to the sensibilities of, well, whoever wrote it. In general, I think it does a good job, but of course not everyone agrees.




回答2:


you can tweak the range of x using the xlim tag. For example, try

hist(d,xlim=c(-10,10))



回答3:


Two suggestions:

#See if this is sufficient:
hist(...)
box()

#If not, try custom axes:
hist(..., xlim = c(-.5, .5), axes = F)
box()
axis(1, seq(-.5, .5, length = 6))
axis(2, seq(0, 30, by = 5))


来源:https://stackoverflow.com/questions/11544051/histogram-in-r-x-axis-not-centered-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!