2

I need a histogram for my data, but could not find one with a curve. Can anyone please suggest a histogram showing frequencies (not densitities) with a curve for the data below? Fancy ones are preferred, but no worries if not :)

x <- rnorm(1000)
hist(x)
Günal
  • 751
  • 1
  • 14
  • 29

3 Answers3

7

Here's the slow, step-by-step version.

This is your data.

population_mean <- 0
population_sd <- 1
n <- 1000
x <- rnorm(n, population_mean, population_sd)

These are some x coordinates for drawing a curve. Notice the use of qnorm to get lower and upper quantiles from a normal distribution.

population_x <- seq(
  qnorm(0.001, population_mean, population_sd), 
  qnorm(0.999, population_mean, population_sd), 
  length.out = 1000
)

In order to convert from density to counts, we need to know the binwidth. This is easiest if we specify it ourselves.

binwidth <- 0.5
breaks <- seq(floor(min(x)), ceiling(max(x)), binwidth)

Here's our histogram.

hist(x, breaks)

The count curve is the normal density times the number of data points divided by the binwidth.

lines(
  population_x, 
  n * dnorm(population_x, population_mean, population_sd) * binwidth, 
  col = "red"
)

Let's see that again with the sample distribution rather than the population distribution.

sample_mean <- mean(x)
sample_sd <- sd(x)
sample_x <- seq(
  qnorm(0.001, sample_mean, sample_sd), 
  qnorm(0.999, sample_mean, sample_sd), 
  length.out = 1000
)
lines(
  population_x, 
  n * dnorm(sample_x, sample_mean, sample_sd) * binwidth, 
  col = "blue"
)

histogram with frequency curves

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
4

Maybe something like this...?

set.seed(1)
x <- rnorm(1000)
hist(x, las=1)

par(new=TRUE)   
plot(density(x), col=2, yaxt="n", xaxt="n",
     bty='n', xlab="", ylab="", main='')
axis(4, las=1)

enter image description here

Frequency is depicted in the left y axis and Probability (for the density line) is in the right y axis.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
-1

Normally you would use a density curve.

Try this:

x <- rnorm(1000)
hist(x)
curve(dnorm, col = 2, add = TRUE)
MartinR
  • 545
  • 1
  • 9
  • 16
  • The histogram look OK, but the curve does not seem to be working I suppose? – Günal Jan 23 '13 at 16:35
  • Oh well, you wanted a histogram showing frequencies, not just density, my mistake. Richie Cotton's answer is straight on. – MartinR Jan 24 '13 at 13:56