R language, the histogram production --hist ()

Histogram
histograms in the x-axis range is divided into a number of groups, displaying the corresponding frequency on the Y axis, shows the distribution of continuous variables. Its function and format
a hist (x)
the parameter x is a continuous variable zero, hist () only x This continuous variable is mandatory
parameters freq = FALSE shows a probability density instead of graphics data drawn frequency (y-axis shows the probability density value instead of frequency)
parameters for the number of breaks in the control group. When defining the histogram unit generates the default segmentation equidistant
example 1, a simple histogram
hist (mtcars $ mpg)
Here Insert Picture Description
Example 2 and the number of colors specified group

hist(mtcars$mpg,breaks = 12,col="blue",
     xlab = "Miles Per Gallon",
     main = "Colored histogram with 12 bins")

Here Insert Picture Description
Example 3, making the probability density pattern to be added FIG axis line and the probability density
axis to be a one-dimensional FIG rendering of the actual value
if there are many junctions (just like the value of) these parameters can be used amout junction axis to be separated in FIG.
such will add a small random value (± a uniformly distributed random number between amout.) each data point
was produced by the function of FIG shaft shall rug (jitter ())

hist(mtcars$mpg,breaks = 12,col="blue",
     xlab = "Miles Per Gallon",
     main = "Colored histogram with 12 bins",freq = FALSE)
#添加轴须图
rug(jitter(mtcars$mpg))
#density()核密度函数给定一个向量可以生成一个核密度估计
lines(density(mtcars$mpg),col="blue",lwd=2)

Here Insert Picture Description

Example 4, was added and the frame curve is too density

#4,添加正太密度曲线和外框
h <- hist(mtcars$mpg,breaks = 12,col="blue",
     xlab = "Miles Per Gallon",
     main = "Colored histogram with 12 bins")
x <- mtcars$mpg
xfit <- seq(min(x),max(x),length=40)
xfit
#生成正太密度曲线
yfit <- dnorm(xfit,mean = mean(x),sd = sd(x))
#对概率密度值进行修正让他可以匹配直方图
#hist()中的mids为每个分组的中心点,diff是为了获得相邻两项的差
#这样处理使得正太密度曲线所围成的图形的面积等于直方图的面积,便于正太密度曲线与直方图的比较
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit,yfit,col="black",lwd=2)
#使用box()函数生成盒型图
box()

Here Insert Picture Description

Example 5, the nuclear density maps
kernel density estimation is used to estimate the random variable
method nuclear density maps of
plot (density (x))
because the plot () function creates a new graphic, so would like an existing graphics Add a density curve on the graph can be used lines () function
plotgon () function draws a polygon based on the coordinates x and y, which may fill the entire graphic col parameter, border parameter is used to fill color boundaries. And this is plotgon () function with a difference plot () function. plot () function refers to a line, plotgon () function refers to a line enclosed pattern.

d <- density(mtcars$mpg)
plot(d)

plot(d,main="Kernel Density of Miles Per Gallon")
#将曲线改成蓝色,并使用实心红色填充曲线下方的区域
#polygon()函数根据x和y坐标绘制多变型
#border表示边界的颜色
polygon(d,col="red",border = "blue")

Here Insert Picture Description
Examples 5, plural sets of the nuclear density map produced
using sm package sm.density.compare () function can be two or more superimposed to FIG density pattern, format
sm.density.compare (x, factor)
wherein x is a numeric vector, factor is a categorical variable

install.packages("sm")
library(sm)
attach(mtcars)
#创建分组变量
cyl.f <- factor(cyl,levels = c(4,6,8),labels = c("4 cylinder","6 cylinder", "8 cylinder"))
#绘制密度图
sm.density.compare(mpg,cyl.f,xlab="Miles Per Gallon")
title(main="Kernel Density of Miles Per Gallon")
#通过鼠标点击添加图例
#生成一个代表颜色的向量
colfill <- c(2:(1+length(levels(cyl.f))))
#levels(cyl.f)表示图例的标签
legend(locator(1),levels(cyl.f),fill=colfill)

Here Insert Picture Description
End summary histogram plotted functions are a hist () function, the parameter value of breaks may be provided for the variable format packet x
freq may be controlled to generate a histogram or probability density histogram generation frequency.
FIG shaft may be generating functions rug (jitter ()) function.
Can be added through the probability density curve after treatment and are too distribution curve comparison on the number of frequency of the y-axis of the histogram
plot (density (x)) may generate nuclear density map
polygon may generate polygon rendering in accordance with the x and y coordinates
sm sm.density.compare package () function may generate a plurality of sets of the nuclear density map which format
sm.density.compare (x, factor)

Published 39 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42712867/article/details/96091693