如何在 R 中创建核密度图?
在本文中,我们将讨论如何用 R 编程语言创建核密度图。为此,用户只需调用 density()函数,该函数是 R 语言中的内置函数。然后用户必须将给定的数据作为参数传递给该函数,以创建给定数据的密度图,作为回报,该函数将返回给定数据的密度图。
通过密度()函数参数的简单变化,用户可以增强给定数据的密度图并更好地理解它。
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.
让我们首先为某些数据创建一个一般密度图,而无需出于增强目的进行任何修改。
例子:
R
# 500 random numeric data
gfg <-rnorm(500)
plot(density(gfg))
R
gfg <-rnorm(500)
plot(density(gfg),main = "GFG Kernel Density Plot",
xlab = "X-Axis",ylab = "Y-Axis")
R
gfg <-rnorm(500)
plot(density(gfg))
polygon(density(gfg), col = "#14e058")
R
gfg <-rnorm(500)
plot(density(gfg))
abline(v = mean(gfg), col = "red")
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")
R
gfg <-rnorm(500)
hist(gfg, prob = TRUE)
lines(density(gfg), col = "#006400")
输出:
使用 density()函数用户可以很容易地用 R 语言绘制核密度曲线,但要修改主标题和轴标签用户需要包含xlab/ylab作为绘图函数的参数,这将有助于用户修改轴标签和修改主标题,用户需要添加main作为绘图函数的参数,这将导致修改 R 语言密度图的主标题和轴标签。
- main:情节的总体标题。
- xlab: x 轴的标题。
- ylab: y 轴的标题。
例子:
电阻
gfg <-rnorm(500)
plot(density(gfg),main = "GFG Kernel Density Plot",
xlab = "X-Axis",ylab = "Y-Axis")
输出:
要在密度图下方创建多边形,用户需要结合使用多边形函数和密度函数,这里的多边形函数用于创建密度图下方的多边形,并且密度()函数用于创建密度给定数据的绘图。
polygon()函数有助于绘制顶点在 x 和 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.
例子:
电阻
gfg <-rnorm(500)
plot(density(gfg))
polygon(density(gfg), col = "#14e058")
输出:
要将平均线垂直添加到密度图,用户需要使用所需参数调用 abline()函数,该函数将返回数据平均值处的密度图的垂直线。
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
例子:
电阻
gfg <-rnorm(500)
plot(density(gfg))
abline(v = mean(gfg), col = "red")
输出:
要在单个图中创建多个核密度图,用户需要使用传递给此函数的col 参数的 line函数来区分密度图线,然后使用密度函数在单个图中绘制所有给定多个图的密度在 R 语言中。
lines()是一个通用函数采用以各种方式给出的坐标并将相应的点与线段连接起来。
Syntax:
lines(x, …)
Parameters:
- x:-coordinate vectors of points to join.
- …:-Further graphical parameters
例子:
电阻
#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")
输出:
要将直方图与密度图叠加,用户首先需要调用 hist()函数并传入所需的参数来构建直方图,然后他/她需要结合 line函数调用密度函数来构建直方图。用R语言构建数据的密度图。
hist()是一个通用函数,用于计算给定数据值的直方图。
Syntax:
hist(x, …)
Parameters:
- x:-a vector of values for which the histogram is desired.
- …:-further arguments and graphical parameters
例子:
电阻
gfg <-rnorm(500)
hist(gfg, prob = TRUE)
lines(density(gfg), col = "#006400")
输出: