The Histogram in R Programming is very useful to visualize the statistical information, that is organized in user specified bins (range, or breaks). Though it looks like Barplot, Histograms display data in equal intervals.

In this article we will show you, How to Create a Histogram in R, Remove it Axes, Format its color, adding labels, adding the density curves, and drawing multiple Histograms in R Programming language with example.

## Histogram in R Syntax

The basic syntax to draw the Histogram in R Programming is as shown below

hist(x, col = NULL, main = NULL, xlab = xname, ylab)

and the complex syntax behind this R Histogram is:

hist(x, breaks = "Sturges", freq = NULL, probability = !freq, xlim = range(breaks), ylim = NULL, col = NULL, angle = 45, include.lowest = TRUE, right = TRUE, density = NULL, main = NULL, xlab = xname, ylab, border = NULL, axes = TRUE, plot = TRUE, labels = FALSE, nclass = NULL, warn.unused = TRUE,..)

Before we get into the example, let us the see the data that we are going to use for this Histogram example. * airquality* is the date set provided by the R

## Return Value of a Histogram in R Programming

In general, before we start creating a Histogram in R, let us see how the data is divided by the histogram.

The Histogram in R will return the frequency (count), density, bin (breaks) values, and type of graph.

In this example we will show you, How to get the information of the same

**R CODE**

# R Histogram Data airquality return_Value <- hist(airquality$Temp) return_Value

**OUTPUT**

## Create a Histogram in R Programming

In this example we will show you, How to create a Histogram using the * airquality* data set, which is provided by the R Studio. If your requirement is to import data from external files then, I suggest you to refer R Read CSV article to understand the csv file import

**R CODE**

# Create a R Histogram airquality hist(airquality$Temp)

**OUTPUT**

**ANALYSIS**

* airquality* data set will return the output as a List. So, we are using the

**$**to extract the data from List.

hist(airquality$Temp)

### Assigning names to Histogram in R Programming

In this example we will show you, How to assign names to Histogram, X-Axis, and Y-Axis using **main****, xlab**, and** ylab**

**main:**You can change, or provide the Title for your Histogram.**xlab:**Please specify the label for the X-Axis**ylab:**Please specify the label for the Y-Axis**las:**It is used to change the Y axis values direction

**R CODE**

# R Histogram Example - Changing Axis Names airquality hist(airquality$Temp, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Frequency", las = 1 )

**OUTPUT**

### Change Colors of a Histogram in R

In this example we will show you, How to change the Histogram color using * col* argument

**col:**Please specify the color you want to use for your Histogram. Typein your console to get the list of colors available in R programming**colors()**

**R CODE**

# R Histogram Example - Changing Colors airquality hist(airquality$Temp, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Frequency", las = 1, col = c("skyblue", "chocolate2") )

From the above code snippet you can observe that, we used two colors for the col argument. It means, those two colors will be repeated until the end of bars.

**OUTPUT**

### Remove Axis and Adding labels to Histogram in R

In this example we will show you, How to remove the X-Axis, Y-Axis, and how to assign labels to each bar in histogram using * axes*,

*, and*

**ann***argument.*

**labels****axes:**It is a Boolean argument. If it is TRUE, axis is drawn.**labels:**It is a Boolean argument. If it is TRUE, Histogram will return the value of each bar on top it.**ann:**It is a Boolean argument. If it is FALSE, Histogram will remove the annotations from the plot area, which includes Histogram name, Axis Names.

**R CODE**

# R Histogram Example - Removing Axis Labels airquality return_Value <- hist(airquality$Temp) return_Value hist(airquality$Temp, axes = FALSE, ann = FALSE, labels = TRUE, ylim = c(0, 35), col = c("skyblue", "chocolate2") )

**OUTPUT**

### Change Axis limits of a Histogram in R

In this example we will show you, How to change the default axis values, and also changing the bar density using * density* argument

**xlim:**This argument can help you to specify the limits for the X-Axis**ylim:**This argument may help you to specify the Y-Axis limits. In this example, We are changing the default y-axis values (0, 35) to (0, 40)**density:**Please specify the shading lines density (in lines per inch). By default it is NULL, means no shading lines.

**R CODE**

# R Histogram Example - Changing Axis Values airquality return_Value <- hist(airquality$Temp) return_Value hist(airquality$Temp, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Frequency", las = 1, col = c("skyblue", "chocolate2"), xlim = c(55, 100), ylim = c(0, 40), density = 80 )

**OUTPUT**

### Changing Bins of a Histogram in R

In this example we will show you, How to change the Bin size using * breaks* argument

- You can use a Vector of values that specify the break points between histogram cells.
- You can use a Number that specify the number of cells a histogram has to return. For example, breaks = 20 means, 20 bars will be returned.
- You can use a function that returns a Vector of break points.

**R CODE**

# R Histogram Example - Changing Bins airquality return_Value <- hist(airquality$Temp) return_Value hist(airquality$Temp, breaks = 20, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Frequency", las = 1, col = c("skyblue", "chocolate2"), labels = TRUE, ylim = c(0, 25) )

**OUTPUT**

## Create a R Histogram with Density

Frequency will count and give us the number of data points per bin. In real-time we are more interested in density than the frequency based histograms because, density can give the probability densities.

In this example we will show you, How to create a Histogram in R against the Density and to achieve the same, we have set the * freq* argument to FALSE.

# R Histogram Example - Density Values airquality return_Value <- hist(airquality$Temp) return_Value hist(airquality$Temp, freq = FALSE, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Density", las = 1, col = c("skyblue", "chocolate2") )

**OUTPUT**

### Adding Density Curve to Histogram in R

In this example we will show you, How to add the density curve to the Histogram using * lines* function.

# R Histogram Example - Add Density Curve airquality hist(airquality$Temp, freq = FALSE, main = "Temperature Histogram", xlab = "Temperature", ylab = "Temperature Density", las = 1, col = c("skyblue", "chocolate2") ) lines(density(airquality$Temp), lwd = 4, col = "red")

**OUTPUT**

**ANALYSIS**

Following statement will draw a density curve

lines(density(airquality$Temp), lwd = 4, col = "red")

**TIP:** lwd argument is used to change the width of the line

## Add Multiple Histograms in R

In this R Histogram example we will show you, How to add multiple Histogram to plot region.

# R Histogram Example - Multiple Histograms airquality # Drawing Histogram for all the temperature values, where Month Number = 7 hist(airquality$Temp[airquality$Month == 7], main = "Multiple Histograms", xlab = "Temperature", ylab = "Temperature Frequency", las = 1, breaks = 20, col = "skyblue", labels = T ) # Drawing Histogram for all the temperatures, where Month Number = 8 hist(airquality$Temp[airquality$Month == 8], add = TRUE, col = "chocolate2", breaks = 20, labels = T )

**OUTPUT**

## Creating R Histogram using CSV File

In this example we will show you, How to create a Histogram in R using the external data. For this, we are importing data from the csv file using read.csv function. please refer R Read CSV article.

# R Histogram Example - CSV File employee <- read.csv("Products.csv", TRUE, sep = ",", na.strings = TRUE) employee$SalesAmount hist(employee$SalesAmount, main = "Sales Histogram", xlab = "Sale Amount", ylab = "Sales Frequency", las = 1, col = "skyblue" )

Above code snippet will draw the histogram in R for the Sales Amount

**OUTPUT**

Thank You for Visiting Our Blog