Skip to content

Histogram for factors

7 messages · Thomas J Vogels, Peter Dalgaard, Martin Maechler +2 more

#
Hi,

I keep running into this:
R> hist (f)
Error in hist.default(f) : `x' must be numeric

To which of course something like (simplified but not beyond repair):
R> hist.factor <- function (ff) {
  jj <- table (ff)
  jb <- barplot (jj, ylab="Frequency", xlab=deparse(substitute(ff)))
  axis (1, jb, names (jj))
}
R> hist (f)
is a possible solution.

Why is a 'hist.factor' not part of the base distribution?  Is this
kind of thing discouraged?  Usually not needed?  Am I missing sth else?

Thanks,
  -tom
#
Thomas Vogels <tov at ece.cmu.edu> writes:
Well histograms are density estimates defined for continuous
variables. What you're drawing is a barplot. barplot.factor might be a
good idea, though.
#
PD> Thomas Vogels <tov at ece.cmu.edu> writes:
    >> Hi,
    >> 
    >> I keep running into this:
    R> hist (f)
    >> Error in hist.default(f) : `x' must be numeric
    >> 
    >> To which of course something like (simplified but not beyond
    >> repair):
    R> hist.factor <- function (ff) {
    >> jj <- table (ff) jb <- barplot (jj, ylab="Frequency",
    >> xlab=deparse(substitute(ff))) axis (1, jb, names (jj)) }
    R> hist (f)
    >> is a possible solution.
    >> 
    >> Why is a 'hist.factor' not part of the base distribution?  Is this
    >> kind of thing discouraged?  Usually not needed?  Am I missing sth
    >> else?

    PD> Well histograms are density estimates defined for continuous
    PD> variables. What you're drawing is a barplot. barplot.factor might
    PD> be a good idea, though.

Well,
	plot(f) 
already draws a barplot of a factor f (by the  plot.factor method).

Martin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Sat, Jan 13, 2001 at 10:31:16AM +0100, Martin Maechler wrote:
Which many of us would think of as being the right "default" plot
for a factor. However, I also think that the right thing for histogram
to do is to inform you that this is not a continuous variable.
I don't think of a barplot (barchart) as a specialized histogram.

  
    
2 days later
#
Robert Gentleman <rgentlem at jimmy.harvard.edu> writes:
[snip]
thanks for the responses.  Yepp, I fell into the trap of trying to use
histogram for a definitely not continuous variable.  I'll go back to
reading a couple of my intro books...

Now the (actually not so) funny thing is that the labels for the
x-axis disappear when I specify colors for the bars.  (R 1.2.1, Linux)
Ok, maybe the other thing I'll have to catch up on is the docu...

Example:
  x <- cut (rnorm (10), 3)
  plot (x)  # has x axis (i.e. cut ranges in this silly example)
  plot (x, col="green")  # no x axis.  Dto. for col=c("green", "red") etc.

Regards,
  -tom
#
Thomas Vogels <tov at ece.cmu.edu> writes:
The logic around "axisnames" in plot.factor looks not quite right.
Could you file a bug report on this so that we dont forget?

barplot(table(factor(1:3)),col="green")
plot(factor(1:3),col="green",axes=T)

both work, although the latter is clearly silly.
#
On Tue, 16 Jan 2001, Thomas Vogels wrote:

            
funny ...
_                
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    1                
minor    1.1              
year     2000             
month    August           
day      15               
language R                

jan