Histogram in R - x-axis not centered properly

后端 未结 3 1901

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

hist(d)

And this is what I get:

相关标签:
3条回答
  • 2021-01-21 03:38

    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)
    

    enter image description here

    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.

    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2021-01-21 04:01

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

    hist(d,xlim=c(-10,10))
    
    0 讨论(0)
提交回复
热议问题