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
First, load the lattice package:
library(lattice)
By default, this package creates a relative frequency histogram with percent along the y-axis:
#create data data <- c(0, 0, 2, 3, 4, 4, 5, 6, 7, 12, 12, 14) #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.
a relative frequency histogram by definition is normalized by height, so it shows proportion. This is just a regular frequency histogram.
Thx for your help.
Is it possible to place above each column a value of respective relative frequency?
Thx for your help. Is it possible to place above each column a value of respective relative frequency?
Very useful site, thx a lot.
Is it possible to place above each column a value of respective relative frequency?
Yes, you can add the values of the relative frequencies above each column in a relative frequency histogram in R. Here’s how you can do it using the **`ggplot2`** package, which offers a lot of flexibility for customizing plots.
### Steps:
1. Compute the relative frequencies.
2. Create the histogram using `geom_bar`.
3. Add the relative frequency values as text labels above each bar using `geom_text`.
### Example Code:
“`R
# Load the ggplot2 library
library(ggplot2)
# Sample data
data <- c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5) # Create a data frame with relative frequencies df <- as.data.frame(table(data)) df$RelativeFrequency <- df$Freq / sum(df$Freq) # Plot the histogram ggplot(df, aes(x = data, y = RelativeFrequency)) + geom_bar(stat = "identity", fill = "skyblue", color = "black") + geom_text(aes(label = round(RelativeFrequency, 2)), vjust = -0.5, # Position text slightly above the bars size = 4, color = "black") + labs(title = "Relative Frequency Histogram", x = "Data", y = "Relative Frequency") + theme_minimal() ``` ### Explanation: 1. **`table(data)`**: Computes the frequency of each unique value. 2. **`df$RelativeFrequency <- df$Freq / sum(df$Freq)`**: Calculates the relative frequency. 3. **`geom_bar(stat = "identity")`**: Plots the bars using pre-calculated values (identity). 4. **`geom_text`**: Adds the text labels (`label`) with the rounded relative frequencies. ### Output: Each bar in the histogram will have its relative frequency displayed as a label above it.