Search notes:

R data visualization: histogram

A most simple example

The most important R function to create a histogram is hist:
x11()

set.seed(280872)

values <- c(rnorm(10000, 50, 30),
            rnorm( 5000,110, 15))        

hist(values)

# wait for mouse click or enter pressed
z <- locator(1)
Github repository about-r, path: /graphics/data-visualization/histogram/simple.R
You might want to decorate the output with a title and labels.

Adding bins to the histogram

The above example only shows 10 bins. This might be too little to reveal the underlying distribution of the data.
The second argument to hist allows to specify the numbers of bins drawn:
x11()

set.seed(280872)

values <- c(rnorm(10000, 50, 30),
            rnorm( 5000,110, 15))        

# Note the 2nd argument to specify the number of bins
hist(values, 40)

# wait for mouse click or enter pressed
z <- locator(1)
Github repository about-r, path: /graphics/data-visualization/histogram/number-of-bins.R

See also

Data visualization with R
Histograms (descriptive statistics)
Perl module GD::Graph::histogram

Index