Histogram in R Programming

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 drawing multiple Histograms in R Programming language with example.

Histogram in R Syntax

The syntax to draw the Histogram in R Programming is

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 see the data that we are going to use for this Histogram example. airquality is the date set provided by the R

Histogram in R Programming 1

Return Value of a Histogram in R Programming

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

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

# R Histogram Data
airquality

return_Value <- hist(airquality$Temp)
return_Value
Histogram in R Programming 2

Create a Histogram in R Programming

In this example, we create a Histogram using the airquality data set, which is provided by the Studio. 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.

# Create a R Histogram
airquality

hist(airquality$Temp)
Histogram in R Programming 3

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 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: Used to change the Y-axis values direction
# R Histogram Example - Changing Axis Names
airquality

hist(airquality$Temp,
     main = "Temperature Histogram",
     xlab = "Temperature",
     ylab = "Temperature Frequency",
     las = 1
     )
Histogram in R Programming 4

Change Colors of a Histogram in R

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

  • col: Please specify the color you want to use for your Histogram. Type colors() in your console to get the list of colors available in R programming
# 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 repeated until the end of bars.

Histogram in R Programming 5

Remove Axis and Adding labels to Histogram

In this example, we remove the X-Axis, Y-Axis, and how to assign labels to each bar in the 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, Histogram returns the value on top of each bar.
  • ann: It is a Boolean argument. If it is FALSE, Histogram removes the annotations from the plot area, which includes the Histogram name, Axis Names.
# 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")
     )
Histogram in R Programming 6

Change Axis limits of a Histogram

Let us change the default axis values, and also changing the bar density using density argument of R histogram

  • 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 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
     )
Histogram in R Programming 7

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 histogram cells.
  • You can use a number that specifies the number of cells a histogram has to return. For example, breaks = 20 means 20 bars returned.
  • You can use a function that returns a Vector of breakpoints.
# 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)
     )
Histogram in R Programming 8

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 histograms because density can give the probability densities.

In this example, we 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")
     )
Histogram in R Programming 9

Adding Density Curve to Histogram

In this example, we add the density curve to the Histogram in R programming 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")
Histogram in R Programming 11

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 Histograms in R

In this example, we add multiple Histograms 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
     )
Histogram in R Programming 10

Creating R Histogram using CSV File

Let us see 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"
     )

The above code snippet draws the histogram using the CSV file for the Sales Amount.

Histogram in R Programming 12