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

Let us see how to Create a Histogram in R, Remove it Axes, Format its color, adding labels, adding the density curves, and make multiple Histograms in R Programming language with example.

## Create Histogram in R Syntax

The syntax to create the Histogram in R Programming is

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

and the complex syntax behind this to make a Histogram in r 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 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 it, let us see how the data divided by the hist.

The Histogram in R returns the frequency (count), density, bin width (breaks) values, and type of graph. In this example, we show how to get the information on the same

airquality return_Value <- hist(airquality$Temp) return_Value

## Create a Histogram in R Programming

In this example, we create a Histogram in R using the airquality data set, which is provided by the RStudio. If you require import data from external files, then I suggest you refer to R Read CSV article to understand the CSV file import. And also refer Barplot article in R Programming.

airquality hist(airquality$Temp)

airquality data set returns 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 assign names, X-Axis, and Y-Axis using main, xlab, and ylab

- main: You can change, or provide the Title.
- xlab: Please specify the label for the X-Axis
- ylab: Please specify the label for the Y-Axis
- las: Used to change the Y-axis values direction

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

### Change Colors of a Histogram in R

In this example, we change the color using the col argument

- col: Please specify the color you want to use for your Hist graph. Type colors() in your console to get the list of colors available in R programming

# 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 repeated until the end of bars.

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

In this example, we remove the X-Axis, Y-Axis, and how to assign labels to each bar in the rstudio histogram using axes, ann, and labels argument.

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

# 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") )

### Change Axis limits of a Histogram in Rstudio

Let us change the default axis values, and also changing the bar density using density argument. They. are density, xlim, and ylim.

- 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.

# 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 )

### Changing Bins of a Histogram

Let us see how to change the Bin size of R histogram using breaks argument.

- You can use a Vector of values that specify the breakpoints between cells.
- You can use a number that specifies the number of cells it has to return. For example, breaks = 20 means 20 bars returned.
- You can use a function that returns a Vector of breakpoints.

# Changing Bin width 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) )

## Create a R Histogram with Density

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

In this example, we create a this against the Density, and to achieve the same, we have set the freq argument to FALSE.

# 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") )

### Adding Density Curve

In this example, we add the density plot or curve to the Histogram in Rstudio using lines function.

# 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")

The following statement draws a density curve

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

TIP: lwd argument change the width of the line

## Add Multiple hist

In this example, we add multiple Histograms in r to plot region.

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

## Creating R Histogram using CSV File

Let us see how to create a Histogram in rstudio using the external data. For this, we are importing data from the CSV file using read.csv function. Please refer R Read CSV article.

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

The above code snippet computes the hist of the given data in the CSV file for the Sales Amount.