How to create Histograms in R

R Histograms

A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called bins), and the number of observation per bin is represented by the height of the bar. Note that, the shape of the histogram can be different following the number of bins we set. To plot a histogram, we use one of the axes as the frequency or count of values and another axis as the range of values divided into buckets. R provides a hist() function which is used to create histograms. This function takes a vector as an input with some parameters to plot histograms.
Syntax:
hist(v,main,xlab,xlim,ylim,breaks,col,border)
Here, v is a vector containing numeric values main is the title of the chart col is for color of the bar or bins border is for border color xlab is the description of the x-axis xlim is the range of values on the x-axis ylim is the range of values on the y-axis breaks are used to specify the width of each bar

Example: Let’s create a simple histogram using input vector, label, col and border parameters.
# Create data for the graph.
v <-  c(12,10,12,9,2,33,12,51,21,33,10)
# Give the chart file a name.
png(file = "histogram.png")
# Create the histogram.
hist(v,xlab = "Weight",col = "red",border = "green")
# Save the file.
dev.off()
Output:

R Histograms Example1

Range of x and y values xlim and the ylim parameter are used to set the range of values in x-axis and y-axis respectively.

Example: Let's see an example:
# Create data for the graph.
v <- c(12,10,12,9,2,33,12,51,21,33,10)
# Give the chart file a name.
png(file = "histogram_lim_breaks.png")
# Create the histogram.
hist(v,xlab = "Weight",col = "yellow",border = "blue", xlim = c(0,50), ylim = c(0,5),
breaks = 5)
# Save the file.
dev.off()
Output: 
R Histograms Example 2


Histograms with different breaks

We can specify the width of the bar through the breaks parameter. Example 1: Let's see an example to create a histogram with 5 breaks.
# Create data for the graph.
v <- c(12,10,12,9,2,33,12,51,21,33,10)
# Give the chart file a name.
png(file = "histogram_breaks5.png")
# Create the histogram.
hist(v, breaks=5, main="With breaks=5")
# Save the file.
dev.off()
Output:

R Histograms Example 3

Example 2: Let's see another example to create a histogram with 20 breaks:
# Create data for the graph.
v <- c(12,10,12,9,2,33,12,51,21,33,10)
# Give the chart file a name.
png(file = "histogram_breaks20.png")
# Create the histogram.
hist(v, breaks=20, main="With breaks=5")
# Save the file.
dev.off()
Output: 

R Histograms Example 4

Bins with different Color Example:
# Create data for the graph.
v <- c(12,10,12,9,2,33,12,51,21,33,10)
# Give the chart file a name.
png(file = "histogram_col.png")
# Create the histogram.
colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan")
hist(v, right=FALSE, col=colors, main="Histogram of v", xlab="Weight")
# Save the file.
dev.off()
Output: 

R Histograms Example 5

Related Topics

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

4 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.