A histogram is a category of bar chart that shows numerical data distribution. It provides a visual representation of the frequency of data. A histogram consists of vertical bars, while the height of each bar in the histogram corresponds to several items in that category or bin. The range of each bucket or category is user-defined.

They are beneficial when there is a lot of data to analyze and difficult to draw conclusions or data demographics. The histogram is used in various fields such as Data Science, Machine, and deep learning. The most popular use for histograms is in market research, where they can determine the potential demand for a product or service.

## Histograms in R programming language

R provides programmers the ability to create histograms so that they can easily visualize and analyze the trends in the abundance of data they are working with. Now that we know what histograms are, and how they can be used let us see how they can be created in R programming.

`hist()`

function in R

`hist()`

function is used to create a histogram in R. The syntax of this function is as follows:

### Syntax

`hist(v, main, xlab, xlim, ylim, breaks, col, border)`

## Parameters

The following is a description of all the parameters used in the `hist()`

function.

: Vector containing the numerical values â€‹â€‹used to create the histogram.`v`

: Indicates the name of the graph.`main`

: It will set the fill color of the bars.`col`

: Used to set the border color of each bar.`border`

: Used to describe the x-axis.`xlab`

: Specify the range of values â€‹â€‹on the x-axis.`xlim`

: Specify the range of values â€‹â€‹on the y-axis.`ylim`

: Indicate the width of each bar or the number of bars in the chart.`breaks`

However, not all the parameters are mandatory. Programmers can create histograms using a combination of these parameters.

## Return Value

The `hist()`

method calculates the histogram of the given data. If it can be plotted, it creates an object of the class histogram which is first plotted and then returned.

## Explanation

Following are some examples that clarify how to create histograms in **R** using some or all the parameters of the `hist()`

function.

## Histograms Using Only Input Vector

The simplest way to create a histogram in **R** is by only providing the input vector v to the `hist()`

function.

```
# create data for the graph containing weights in kg
weights <- c(75, 80, 85, 82, 81, 82, 76, 79, 73, 85, 74, 76, 86, 72, 75)
# create the histogram.
hist(weights)
```

**Line#2**: declares the input vectorusing`weight`

`c()`

function.**Line#4**: calls the`hist()`

function which plots the histogram using the given vector.

### Output

We get the following histogram as output.

## Histograms With Colored Borders

```
# Create data for the graph containing weights in kg
weights <- c(75, 80, 85, 82, 81, 82, 76, 79, 73, 85, 74, 76, 86, 72, 75)
# Create the histogram.
hist(weights, col = "green", border = "black")
```

**Line#4**: Calls the`hist()`

function but this time, it also defines the fill and border color of the histogram bars by using the`col`

and`border`

parameters respectively.

### Output

We get a histogram given below

## Histograms With Description

We can also alter the label of the x-axis of our histogram using the `xlab`

parameter in the `hist()`

function. By default, the description is the name of your input vector.

```
# Create data for the graph containing weights in kg
weights <- c(75, 80, 85, 82, 81, 82, 76, 79, 73, 85, 74, 76, 86, 72, 75)
# Create the histogram.
hist(weights, xlab="Weights in KG", col = "blue", border = "red")
```

**Line#4**: In addition to calling the`hist()`

function, also declares the description of the histogram using the`xlab`

parameter which is set to**Weights in KG**.

### Output

The output histogram is attached below. Notice that the label on **x-axis** has changed to the one provided in `hist()`

using `xlab`

parameter.

## Histograms With Range and Breaks

We can specify the range of the `x`

and `y-axis`

as well as the number of rectangles in our histogram using the `xlim`

, `ylim`

, and breaks parameters.

```
# Create data for the graph containing weights in kg
weights <- c(75, 80, 85, 82, 81, 82, 76, 79, 73, 85, 74, 76, 86, 72, 75)
# Create the histogram.
hist(weights, xlab="Weights in KG", col = "green", border = "black", xlim = c (70, 90), ylim = c(0, 3), breaks = 10)
```

Line#4: the range of the **x-axis** and **y-axis** has been declared using the `xlim`

and `ylim`

parameters. The breaks parameter is also set to **10** which sets the number of bars in the histogram to **10**.

### Output

Following is the resulting histogram