[R] hist - unevenly spaced bars

Peter Ehlers ehlers at ucalgary.ca
Thu Jan 28 23:09:59 CET 2010


Well, your bars are not unevenly spaced; you just have
some zero-count intervals. Time to learn about the
str() function which will tell you what's going on.

zh <- hist(your_code)
str(zh)
zh$breaks
zh$counts

You could set breaks with

hist(..., breaks=0:5 + .5)

But a histogram doesn't seem like the right thing to do.
Try barplot:

barplot(table(Q5))

  -Peter Ehlers

Worik R wrote:
> I am sure this is trivial, but I cannot solve it.
> 
> I make a histogram.  There are 5 categories "1",...,"5" and 80 values and
> the histogram does not evenly space the bars.
> 
> Bars "1" and "2" have no space between them and the rest are evenly spaced.
> 
> How can I get all bars evenly spaced?
> 
> The code:
> 
>> Q5
>  [1] "4" "4" "4" "5" "2" "4" "5" "3" "4" "5" "3" "4" "3" "5" "2" "4" "5" "5"
> "4"
> [20] "3" "1" "4" "5" "5" "4" "3" "1" "5" "4" "3" "5" "3" "3" "5" "5" "5" "5"
> "4"
> [39] "4" "5" "1" "1" "5" "4" "4" "4" "1" "4" "4" "5" "5" "2" "4" "5" "4" "3"
> "4"
> [58] "5" "1" "2" "1" "5" "4" "5" "5" "1" "4" "1" "4" "5" "1" "4" "5" "5" "4"
> "5"
> [77] "5" "4" "4" "3"
>> hist(as.numeric(Q5),  density=30, main=strwrap(S5, width=60), axes=FALSE)
>> axis(side=1, labels=c("Disagree", "2", "Not Sure", "4", "Strongly Agree"),
> at=c(1, 2, 3, 4, 5))
>> axis(side=2)
> 
> cheers
> Worik
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list