Open In App

Histograms in R language

Last Updated : 13 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A histogram contains a rectangular area to display the statistical information which is proportional to the frequency of a variable and its width in successive numerical intervals. A graphical representation that manages a group of data points into different specified ranges. It has a special feature that shows no gaps between the bars and is similar to a vertical bar graph.

R – Histograms

We can create histograms in R Programming Language using the hist() function.

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

Parameters: 

  • v: This parameter contains numerical values used in histogram.
  • main: This parameter main is the title of the chart.
  • col: This parameter is used to set color of the bars.
  • xlab: This parameter is the label for horizontal axis.
  • border: This parameter is used to set border color of each bar.
  • xlim: This parameter is used for plotting values of x-axis.
  • ylim: This parameter is used for plotting values of y-axis.
  • breaks: This parameter is used as width of each bar.

Creating a simple Histogram in R

Creating a simple histogram chart by using the above parameter. This vector v is plot using hist().

Example: 

R




# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32,
       14, 19, 27, 39)
 
# Create the histogram.
hist(v, xlab = "No.of Articles ",
     col = "green", border = "black")


Output:

Histograms in R languageGeeksforgeeks

Histograms in R language

Range of X and Y values

To describe the range of values we need to do the following steps: 

  1. We can use the xlim and ylim parameters in X-axis and Y-axis.
  2. Take all parameters which are required to make a histogram chart.

Example 

R




# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
 
 
# Create the histogram.
hist(v, xlab = "No.of Articles", col = "green",
    border = "black", xlim = c(0, 50),
    ylim = c(0, 5), breaks = 5)   


Output: 

Histograms in R languageGeeksforgeeks

Histograms in R language

Using histogram return values for labels using text()

To create a histogram return value chart.

R




# Creating data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19,
       27, 39, 120, 40, 70, 90)
 
# Creating the histogram.
m<-hist(v, xlab = "Weight", ylab ="Frequency",
        col = "darkmagenta", border = "pink",
        breaks = 5)
 
# Setting labels
text(m$mids, m$counts, labels = m$counts,
     adj = c(0.5, -0.5))   


Output:

Histograms in R languageGeeksforgeeks

Histograms in R language

Histogram using non-uniform width

Creating different width histogram charts, by using the above parameters, we created a histogram using non-uniform width.

Example 

R




# Creating data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14,
       19, 27, 39, 120, 40, 70, 90)
     
# Creating the histogram.
hist(v, xlab = "Weight", ylab ="Frequency",
     xlim = c(50, 100),
    col = "darkmagenta", border = "pink",
    breaks = c(5, 55, 60, 70, 75,
               80, 100, 140))


Output:

Histograms in R languageGeeksforgeeks

Histograms in R language



Previous Article
Next Article

Similar Reads

Draw Multiple Overlaid Histograms with ggplot2 Package in R
In this article, we are going to see how to draw multiple overlaid histograms with the ggplot2 package in the R programming language. To draw multiple overlaid histograms with the ggplot2 package in R, you can use the geom_histogram() layer multiple times, each with different data and mapping specifications. Here is an example to create multiple hi
7 min read
How To Make Scatterplot with Marginal Histograms in R?
In this article, we will discuss how to make a Scatterplot with Marginal Histograms in the R Language. Marginal Histograms in R: A scatter plot and separate histograms for each variable on their respective axes are both combined in a marginal histogram, which is a visualization approach. Using this method, we can look at the univariate distribution
6 min read
How to Plot Multiple Histograms in R?
In this article, we will discuss how to plot multiple histograms in the R Programming language. Method 1: Multiple Histogram in Base R To create multiple histograms in base R, we first make a single histogram and then add another layer of the histogram on top of it. But in doing so some plots may clip off as axis are made according to the first plo
6 min read
Scatterplot with marginal histograms in ggplot2
Histograms are graphical representations of data distributions, where data is divided into equal intervals called bins and the number of data points falling in each bin is represented by a bar. Histograms are useful for understanding the shape of the data distribution, identifying outliers, and finding patterns or trends.ggplot2 is a plotting packa
8 min read
R - How to plotting log-scaled histograms in plotly
A histogram is a graph that displays the frequency or number of occurrences of different values or ranges of values in a dataset. The x-axis represents the values in the dataset and the y-axis represents the frequency of those values. The range of values is divided into bins, and the height of each bar in the histogram represents the frequency of v
5 min read
Histograms in the Lattice Package
For making trellis or tiny multiple plots, a style of visualization that displays several versions of a plot for subsets of the data, the Lattice package in R is a potent tool. Lattice's histogram() function can be used to generate histograms for continuous variables and includes a number of useful features. Grouping: Using the group argument of th
8 min read
Histograms and Density Plots in R
A histogram is a graphical representation that organizes a group of data points into user-specified ranges and an approximate representation of the distribution of numerical data. In R language the histogram is built with the use of the hist() function. Syntax: hist(v,main,xlab,xlim,ylim,breaks,col,border) Parameters: v:- It is a vector containing
3 min read
Boxplots in R Language
A box graph is a chart that is used to display information in the form of distribution by drawing boxplots for each of them. This distribution of data is based on five sets (minimum, first quartile, median, third quartile, and maximum). Boxplots in R Programming Language Boxplots are created in R by using the boxplot() function. Syntax: boxplot(x,
4 min read
Finding Day and Month on a Specific Date in R Language - weekday() and month() function
weekdays() function in R Language is used to find out a day on a specific date and return as a string. Syntax: weekdays(date)Parameters: x: date abbreviate: logical value Example 1: Weekdays function C/C++ Code # R program to illustrate # weekday function # Create example date date &lt;- as.POSIXlt(&quot;2020-5-19&quot;) # Apply weekdays function w
1 min read
Single-Table Analysis with dplyr using R Language
The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : install.packages("dplyr") Let's create the main dataframe: C/C++ Code #installing the required libraries library(dplyr) #creating a data frame data_frame = data.frame(
5 min read
Article Tags :