How to Create a Relative Frequency Histogram in R? - GeeksforGeeks

04 Jan, 2022
In this article, we will discuss how to create a Relative Frequency Histogram in the R programming language.
Relative Frequency Histogram helps us to visualize the relative frequencies of values in a dataset. This shows how often a certain value is present in the dataset. A relative frequency histogram gives an empirical approximation to the probability density function of data.
To create a Relative Frequency Histogram in the R Language, we use the histogram() function of the lattice package library. The histogram() function takes the data vector as an argument and returns a relative frequency histogram. By default, the Histogram() function provides a color theme according to the current, active theme.
Syntax:
histogram( data, type )
Parameter:
data: determines the data vector to be plotted.
type: determines the type of relative frequency histogram that includes percent, count, and density.
Example: Example of a basic relative frequency histogram with default configuration in the R Language.
R
# load library latticelibrary(lattice)  # create sample data vectorsample_data <- rnorm(100)  # create relative frequency histogramhistogram( sample_data )
Output:
To change the color of the bars of the histogram, we use the col argument of the histogram() function. To change the label of the x-axis and y-axis, we use the xlab and ylab arguments respectively. To add a title to the plot we use the main argument of the histogram() function.
Syntax:
histogram( data, col, xlab, ylab, main )
Parameter:
col: determines the color of bars/bins.
xlab and ylab: determine the label of the x-axis and y-axis respectively.
main: determines the title of plot.
Example: Example of a customized relative frequency histogram with green colored bars and custom labels.
R
# load library latticelibrary(lattice)  # create sample data vectorsample_data <- rnorm(100)  # create relative frequency histogramhistogram( sample_data, col="green", main="Title of Plot",           xlab="X-axis title", ylab="Y-axis title")
Output:
To customize the number of bins/bars in the relative frequency histogram, we can use the breaks argument of the histogram() function. The breaks argument takes a number of bins as value and breaks the plot into that many bars. By using the breaks argument, we can increase as well as decrease the number of bars based on the value.
Syntax:
histogram( data, breaks )
Parameter:
data: determines the data vector to be plotted.
breaks: determines the number of bars in the histogram.
Example: Example of a relative frequency histogram with 20 bars customized using the breaks argument of the histogram() function.
R
# load library latticelibrary(lattice)  # create sample data vectorsample_data <- rnorm(100)  # create relative frequency histogramhistogram( sample_data, breaks=20)
Output:
Picked
R-Charts
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R
R - if statement
How to filter R dataframe by multiple conditions?