-2

I have some discrete data that I'm trying to plot in a histogram in R. I'm using the built in hist() function, which works fine most of the times for the data I have. However, when it comes to a discrete variable it looks somewhat strange (unfortunately I cannot add the picture). I interpret it as "since the bin for 0 and 1 children must fit between 0 and 1 it determines the width of all bins and thus the "from 1.5 to 2" result". How can I put the numbers on the x-axis centered underneath each bin instead?

Thanks in advance!

castarha
  • 1
  • 2
  • Please read [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to ask a good question in R. Particularly, please include a reproducible example by editing the output of ``dput(head(data))`` into your original question. This will help people trying to answer your question. Thanks. – user438383 Jan 31 '21 at 09:34
  • Thanks for letting me know! I'm new to R. I'll read the post and think of that in the future :) – castarha Jan 31 '21 at 15:19

1 Answers1

0

You might want to consider drawing the axis in a second step:

Prevent x-axis with xaxt="n":

hist(cars$speed, xaxt="n")

Draw x-axis and adjust label position with hadj=:

axis(1, at=seq(0,25,5), hadj=2.5, labels = c("",seq(5,25,5)))

example with cars data

pascal
  • 1,036
  • 5
  • 15
  • Thank you! However, I'm only setting the last number, what am I doing wrong? Do I need to do a loop? – castarha Jan 31 '21 at 15:17
  • try to include all numbers you need in the axis function, e.g. `axis(1, at=c(1,2,3,4,5,))` if you want to have your labels at 1-5. – pascal Jan 31 '21 at 15:45