1

I'm trying to plot a histogram for a dataframe in R. I'm using the Hmisc package to do the same. It generates a very nice plot for categorical data except for one problem. It displays the frequencies instead of probabilities. Please find a code sample (from the package documentation, although my data is categorical but that doesn't matter) below:

x <- rnorm(200,0,2)+1; y <- x^2
x2 <- round((x+rnorm(200))/2)*2
x3 <- round((x+rnorm(200))/4)*4
dfram <- data.frame(y,x,x2,x3)
hist(dfram)

How do I modify the plot above to display normalised frequencies?

I have already tried hist(dfram, type='density') but type ='density' is an argument to histSpike function to plot kernel densities. I have also tried hist(dfram,f=F) and hist(dfram,prob=T) (which are essentially the same from what I understand) but the histograms still show frequencies.

Thomas
  • 43,637
  • 12
  • 109
  • 140

1 Answers1

0

Take the output of hist, which contains the normalized frequencies in $density, and plot (e.g. barplot ) those results.
That is,

foo<-hist(dfram)  #although you probably wanted hist(dfram$y) or some such)
barplot(foo$density)
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Hi, I'm using the `hist.data.frame` function from the Hmisc package. I tried your suggest but it didn't work for me. Your suggestion applies for the hist function from the base package. Attaching another example to make the problem even more clear. `a<-data.frame(sample(c(0,1),1000,replace=T),sample(LETTERS[1:2],1000,replace=T))` `hist(a)` – Ajinkya Ghorpade Oct 02 '13 at 13:01
  • @AjinkyaGhorpade Take a look at the help file for `Hmisc::hist` and see what values are returned. Most likely either the densities or at least the bin quantities are available. If the latter, just normalize the bin quantities against themselves. – Carl Witthoft Oct 02 '13 at 13:43
  • I did check the return values. The function just returns number of pages drawn. – Ajinkya Ghorpade Oct 02 '13 at 13:49
  • Then my recommendation would be to use `graphics::hist` for each dataset and then build the multi-graph with `ggplot` or `grid` – Carl Witthoft Oct 02 '13 at 14:52