Home Â» How to Create a Relative Frequency Histogram in R

# How to Create a Relative Frequency Histogram in R

AÂ relative frequency histogram is a graph that displays the relative frequencies of values in a dataset.

This tutorial explains how to create a relative frequency histogram in R by using theÂ histogram() function from theÂ lattice, which uses the following syntax:

histogram(x,Â type)

where:

• x:Â data
• type: type of relative frequency histogram youâ€™d like to create; options includeÂ percent, count, andÂ density.

### Default Histogram

`library(lattice)`

By default, this package creates a relative frequency histogram withÂ percentÂ along the y-axis:

```#create data
data #create relative frequency histogram
histogram(data)```

### Modifying the Histogram

We can modify the histogram to include a title, different axes labels, and a different color using the following arguments:

• main:Â the title
• xlab:Â the x-axis label
• ylab:Â the y-axis label
• col:Â the fill color to use in the histogram

For example:

```#modify the histogram
histogram(data,
main='Points per Game by Player',
xlab='Points per Game',
col='steelblue')
```

### Modifying the Numbers of Bins

We can specify the number of bins to use in the histogram using theÂ breaksÂ argument:

```#modify the number of bins
histogram(data,
main='Points per Game by Player',
xlab='Points per Game',
col='steelblue',
breaks=15)
```

The more bins you specify, the more you will be able to get a granular look at your data. Conversely, the fewer number of bins you specify, the more aggregated the data will become:

```#modify the number of bins
histogram(data,
main='Points per Game by Player',
xlab='Points per Game',
col='steelblue',
breaks=3)
```

Related: Use Sturgesâ€™ Rule to identify the optimal number of bins to use in a histogram.