This article will discuss using the density histogram in the R programming language together. First, we will go to find out what is the histogram and density together.

**What is the histogram in R?**

A histogram is a visual that groups several data points into user-specified ranges and provides a rough idea of the distribution of numerical data. See the syntax for this function below.

**Syntax**:

`hist(x, main, xlab, xlim, ylim, breaks, col, border)`

**Parameters:**

**x:**It is a vector including numeric values.**main:**The title of the chart.**col:**Set the color of the bars.**border:**Set the border color of each bar.**xlab:**Describe the x-axis.**xlim:**The range of values on the x-axis.**ylim:**The range of values on the y-axis.**breaks:**The width of each bar.

**What is the density in R**?

A density plot depicts a numeric variable’s distribution using a kernel density estimate to depict the variable’s probability density function. R Language’s density() function is used to generate kernel density estimates. Furthermore, its return value is utilized to construct the final density map. Follow the syntax below:

**Syntax**:

`density(x)`

**Parameters:**

**x:**The data that needs to be computed.

**How to use the density and histogram in R**?

**Using the hist() function in R to plot**

First, we will create a **vector**** **for this example as follows:

# Create the vector vec <- c(5, 3, 6, 9, 12, 6, 3, 2, 9, 10, 36) # View the vector vec

**Output**

`[1] 5 3 6 9 12 6 3 2 9 10 36`

Now, we will use the hist() function in R to plot the chart with this vector.

# Create the vector vec <- c(5, 3, 6, 9, 12, 6, 3, 2, 9, 10, 36) # Plot the chart hist( vec, xlab = "Weight", ylab = "Frequency", main = "Histogram of vector", col = "green", border = "black" )

**Output**

**Using the density() function in R to plot**

Here, we will use the density() function to plot a graph with the data as an iris dataset. And we will use the ggplot2 package for this example. So, if you don’t have this package, you need to install it.

# Import package library(ggplot2) # Using density() function dens <- density(iris$Sepal.Length) # Plot plot(dens, frame = FALSE, main = "Density", col = "red")

**Output**

**Using the hist() and density() functions in the same frame**

Now, we will use hist() and density() functions in the same frame to plot a graph and use the iris dataset in this example. Follow the code example below to understand.

# Plot with hist() function hist( iris$Sepal.Length, prob = TRUE, main = "Histograms and density", col = "green", border = "black", xlab = "Temp", ylab = "Density" ) # Plot with density() function lines( density(iris$Sepal.Length), lwd = 2, col = "red" )

**Output**

**Summary**

This article demonstrates how to plot with hist() and density functions in the R programming language. If you have any questions, please leave a comment below.

Good luck!

**Maybe you are interested**:

- identity matrix in R: How To Create The Identity Matrix In R
- any() Function In R: Check any values in a vector
- The ln() function in R

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.

**Name of the university: **HCMUS

**Major**: IT

**Programming Languages**: C, C++, Python, R, Java, JavaScript