How to create Kernel Density Plot in R?
Last Updated :
23 May, 2021
In this article, we will discuss how to create kernel density plots in R programming language. For this, the user simply needs to call the density() function which is an in-build function in R language. Then the user has to pass the given data as the parameter to this function in order to create a density plot of the given data and further in return this function will be returning the density plot of the given data.
By simple variation in the parameter of density() function, the user can enhance the density plot of the given data and a better understandability to it.
Syntax: density(x,…)
Parameters:
- x:-the data from which the estimate is to be computed.
- ...:-further arguments for (non-default) methods.
Returns:
This function will be returning the density plot of the given data.
Let us first create a general density plot for some data without any modification for enhancement purposes.
Example:
R
# 500 random numeric data
gfg <-rnorm(500)
plot(density(gfg))
Output:
Using the density() function the user can easily plot the kernel density curve in R language, but to modify the main title and the axis label user need to include xlab/ylab as the parameter of the plot function which will help the user to modify the axis label and to modify the main title, the user needs to add main as the parameter of the plot function and this will lead to modification of main title & axis labels of density plot in R language.
- main: an overall title for the plot.
- xlab: a title for the x-axis.
- ylab: a title for the y axis.
Example:
R
gfg <-rnorm(500)
plot(density(gfg),main = "GFG Kernel Density Plot",
xlab = "X-Axis",ylab = "Y-Axis")
Output:
To create a polygon below the density plot, the user needs to use the polygon function in combination with the density function, here the polygon function is used to create the polygon under the density plot and the density() function is used to create the density plot of the given data.
polygon() function helps to draw the polygons whose vertices are given in x and y.
Syntax:
polygon(x, y = NULL, density = NULL, angle = 45,border = NULL, col = NA, lty = par("lty"), …, fillOddEven = FALSE)
Parameters:
- x, y:-vectors containing the coordinates of the vertices of the polygon.
- density:-the density of shading lines, in lines per inch. The default value of NULL means that no shading lines are drawn.
- angle:-the slope of shading lines, given as an angle in degrees (counter-clockwise).
- col:-the color for filling the polygon. The default, NA, is to leave polygons unfilled unless density is specified.
- border:-the color to draw the border. The default, NULL, means to use par("fg"). Use border = NA to omit borders.
- lty:-the line type to be used, as in par.
- …:-graphical parameters such as xpd, lend, ljoin and lmitre can be given as arguments.
- fillOddEven:-logical controlling the polygon shading mode: see below for details. Default FALSE.
Example:
R
gfg <-rnorm(500)
plot(density(gfg))
polygon(density(gfg), col = "#14e058")
Output:
To add a mean line vertically to the density plot user need to call the abline() function with the required parameter with the density function which will be returning a vertical line of the density plot at the mean value of the data.
Syntax:
abline(a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL, coef = NULL, untf = FALSE, ...)
Parameters:
- a, b:-the intercept and slope, single values.
- untf:-logical asking whether to untransform.
- h:-the y-value(s) for horizontal line(s).
- v:-the x-value(s) for vertical line(s).
- coef:-a vector of length two giving the intercept and slope.
- reg:-an object with a coef method. See ‘Details’.
- …:-graphical parameters such as col, lty and lwd
Example:
R
gfg <-rnorm(500)
plot(density(gfg))
abline(v = mean(gfg), col = "red")
Output:
To create multiple kernel density plots in a single plot user need to use the line function with col parameter passed into this function to differentiate among the density plotline and then using the density function to plot the density of all the given multiple plots in a single plot in R language.
lines() is a generic function taking coordinates given in various ways and joining the corresponding points with line segments.
Syntax:
lines(x, …)
Parameters:
- x:-coordinate vectors of points to join.
- …:-Further graphical parameters
Example:
R
#500 random numeric data
gfg <-rnorm(500)
a <- rnorm(200)
b <- rnorm(100)
plot(density(gfg))
lines(density(a), col = "red")
lines(density(b), col = "green")
Output:
To overlay a histogram with the density plot, the user first needs to call the hist() function with the required parameters pass into it to build the histogram, further, he/she needs to call the density function in the combination of line function to build the density plot of the data in R language.
hist() is a generic function used to compute a histogram of the given data values.
Syntax:
hist(x, …)
Parameters:
- x:-a vector of values for which the histogram is desired.
- …:-further arguments and graphical parameters
Example:
R
gfg <-rnorm(500)
hist(gfg, prob = TRUE)
lines(density(gfg), col = "#006400")
Output:
Similar Reads
How to Create a Forest Plot in R?
In this article, we will discuss how to create a Forest Plot in the R programming language. A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in med
4 min read
How to Create Interaction Plot in R?
In this article, we will discuss how to create an interaction plot in the R Programming Language. The interaction plot shows the relationship between a continuous variable and a categorical variable in relation to another categorical variable. It lets us know whether two categorical variables have a
3 min read
Overlay Density Plots in Base R
In this article, we will discuss how to overlay density plot using some base functions of the R Programming Language. Overlaying density plots means creating some density plots of the different data over a single plot. Functions UsedPlot() : This is a generic function for plotting R objects. Syntax:
2 min read
How to Create a Log-Log Plot in R?
In this article, we will discuss how to create a Log-Log plot in the R Programming Language. A log-log plot is a plot that uses logarithmic scales on both the axes i.e., the x-axis and the y-axis.We can create a Log-Log plot in the R language by following methods. Log-Log Plot in Base R: To create a
2 min read
How To Create Dot Plots In Excel?
A Dot plot is a type of chart used in statistics for representing relatively small data sets where the values are uniquely categorized. A dot plot is also known as dot chart or strip chart. A dot plot and a bar graph are similar in the sense that the height of each âbarâ of dots is equal to the numb
2 min read
How to Draw Poisson Density Curve in R?
In this article, we are going to learn how to draw the Poisson density curve in the R programming language. Function used: dpois(): The dpois() method is used to accept the vector of quantiles and the vector of means as the input parameters. The dpois() method has the following syntax :Â Syntax: dpo
2 min read
How to Create a Frequency Polygon in R?
In this article, we will discuss how to create a Frequency Polygon in the R Programming Language. Frequency polygons are the plots of the values in a data frame to visualize the shape of the distribution of the values. It helps us in comparing different data frames and visualizing the cumulative fre
3 min read
How To Make Density Plots with ggplot2 in R?
Density plots are a data visualization method used to estimate the probability density function (PDF) of a continuous variable. They provide smooth, continuous data distribution which makes them more informative than histograms in certain situations. The plot is produced by applying a kernel functio
2 min read
How to plot excel data in R?
Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read
How to Plot a Log Normal Distribution in R
In this article, we will explore how to plot a log-normal distribution in R, providing you with an understanding of its properties and the practical steps for visualizing it using R Programming Language.Log-Normal DistributionThe log-normal distribution is a probability distribution of a random vari
5 min read