R |中的直方图了解如何使用 R 软件创建直方图

R |中的直方图了解如何使用 R 软件创建直方图,第1张

R |中的直方图了解如何使用 R 软件创建直方图,R 中的直方图,第2张

R 中的直方图简介

R 中的直方图是图形数据表示和数据分析的首选图之一。直方图通常被视为在二维轴上对齐的垂直矩形,显示数据类别或组的比较。条形或矩形框的高度在 y 轴上显示数据计数,数据类别值在 x 轴上保持。直方图有助于探索性数据分析。可以为数据集的特定变量创建 R 中的直方图,这对于数据科学项目中的变量选择和特征工程实现非常有用。R 语言支持开箱即用的包来创建直方图

多合一数据科学包(360+ 课程, 50+ 项目)
R |中的直方图了解如何使用 R 软件创建直方图,蟒蛇教程,第3张R |中的直方图了解如何使用 R 软件创建直方图,机器学习,第4张R |中的直方图了解如何使用 R 软件创建直方图,自主技术,第5张R |中的直方图了解如何使用 R 软件创建直方图,人工智能,第6张R |中的直方图了解如何使用 R 软件创建直方图,画面,第7张R |中的直方图了解如何使用 R 软件创建直方图,R 编程,第8张R |中的直方图了解如何使用 R 软件创建直方图,PowerBI,第9张R |中的直方图了解如何使用 R 软件创建直方图,深度学习,第10张价格
$99 99
查看课程 360+ 在线课程 |50+ 项目 |1500+ 小时|可验证证书|终身访问
4.7 (83,774 评价)

什么是直方图?

直方图是数据集分布的图形表示,通过它我们可以轻松分析哪个因子的数据量更高,数据最少。换句话说,直方图允许在x轴和y轴上进行累积频率图。实际上,直方图同时采用分组和未分组的数据。对于分组数据,直方图是通过考虑类边界来构建的,而未分组的数据则需要形成分组的频率分布。它们有助于有效地分析数据的范围和位置。直方图的一些常见结构在数据分布期间应用,如正态、偏斜、悬崖。

与条形不同,图表直方图在条形之间没有间隙,此处的条形被命名为条柱,数据以相等的间隔表示。直方图 采用连续变量并拆分为区间,必须选择正确的箱宽。条形图和直方图之间的主要区别在于前者使用标称数据集进行绘制,而直方图绘制连续数据集。R 使用 hist () 函数创建直方图。此 hist () 函数使用值向量来绘制直方图。直方图由连续值的 x 轴范围组成,y 轴在 x 轴上绘制数据的频繁值,并带有高度变化的条形。

语法:

创建直方图的语法为

hist (v, main, xlab, xlim, ylim, breaks,col,border)
where v – vector with numeric values
main – denotes title of the chart
col – sets color
border -sets border color to the bar
xlab - description of x-axis
xlim - denotes to specify range of values on x-axis
ylim – specifies range values on y-axis
break – specifies the width of each bar.

在 R 中创建直方图

为了进行分析,目的直方图需要一些内置数据集才能导入R.R及其库具有各种图形包和函数。在这里,我们使用瑞士和航空乘客数据集。要计算给定数据值的直方图,hist () 函数与 $ 符号一起使用,从数据集中选择数据的某一列以创建直方图。

下面的示例计算名为 Swiss 的数据集的“检查”列中的数据值的直方图。

示例 #1 – 创建简单的直方图

法典:

hist (swiss $Examination)

输出:

R |中的直方图了解如何使用 R 软件创建直方图,R 1 中的直方图,第11张

Hist 是为具有列检查的数据集瑞士创建的。这只是绘制一个带有频率和 x 轴的箱。

示例 #2 – 具有更多参数的直方图

为了更好地理解直方图,我们需要向 hist 函数添加更多参数以优化图表的可视化。将 x 和 y 标签更改为一系列值 xlim 和 ylim 参数将添加到函数中。

例:

hist (Air Passengers, xlim=c (150,600), ylim=c (0,35))
In the above example x limit varies from 150 to 600 and Y – 0 to 35.
// Adding breaks
hist (AirPassengers,
main="Histogram with more Arg",
xlab="Name List",
border="Green",
col="Orange",
xlim=c (100,600),
ylim=c(0,40),
breaks=5)

输出:

R |中的直方图了解如何使用 R 软件创建直方图,R 2 中的直方图,第12张

上面的代码图是数据集 Air Passengers 中值的直方图,标题为“更多参数的直方图”,x 轴标签为“名称列表”,条形带有绿色边框和黄色,通过将值限制为 100 到 600,将打印在 y 轴上的值限制为 2 并使箱宽为 5。

向条形添加两种不同的颜色

hist (swiss$Examination, col=c ("violet”, "Chocolate2"), xlab="Examination”, las =1, main=" color histogram")

输出:

R |中的直方图了解如何使用 R 软件创建直方图,R 3 中的直方图,第13张

向直方图添加更多条形

hist (swiss$Education, breaks=40, col="violet", xlab="Education", main=" Extra bar histogram")

输出:

R |中的直方图了解如何使用 R 软件创建直方图,R 4 中的直方图,第14张

示例 #3 – R 中的直方图 它返回一个值

Air <- AirPassengers
hist (Air)
h <- hist (Air)
h
$breaks

输出:

R |中的直方图了解如何使用 R 软件创建直方图,R 5 中的直方图,第15张

示例#4 –使用中断参数更改箱宽度

要在宽度之间有更多的断点,最好使用 c() 函数中的值。

hist (AirPassengers, breaks=c (100, seq (200,700, 150)))

输出:

R |中的直方图了解如何使用 R 软件创建直方图,H.R 6,第16张

The above graph takes the width of the bar through sequence values.

Example #5 – Implementing the Normal Distribution Curve in Histogram

We shall use the data set 'swiss’ for the data values to draw a graph. Here the function curve () is used to display the distribution line.

Code:

curve (dnorm(x, mean=mean(swiss$Education), sd=sd(swiss$Education)), add=TRUE, col="red")

Output:

R |中的直方图了解如何使用 R 软件创建直方图,H 7,第17张

Example #6 – Plotting Probability Distribution

hist (AirPassengers,
main="Histogram ",
xlab="Passengers",
border="Yellow",
col="pink",
xlim=c(100,600),
las=2,
breaks=6,
prob = TRUE)

Creating Density Plots in Histogram in R

The distribution of a variable is created using function density (). Below is the example with the dataset mtcars. Density plots help in the distribution of the shape.

density () // this function returns the density of the data
library(ggplot2)
d <- density (mtcars $qsec)
plot (d, main=" Density of Miles Per second")
polygon (d, col="orange", border="blue")

Output:

R |中的直方图了解如何使用 R 软件创建直方图,H 8,第18张

Using Line () function
hist (swiss$Examination, freq = FALSE, col=c ("violet”, "Chocolate2"),
xlab="Examination”, las =1, main=" Line Histogram")
lines(density(swiss$Examination), lwd = 4, col = "red")

The following histogram in R displays the height as an examination on x-axis and density is plotted on the y-axis.

Output:

R |中的直方图了解如何使用 R 软件创建直方图,H 9,第19张

Conclusion

That’s all about the histogram and precisely histogram is the easiest way to understand the data. As we have seen with a histogram, we could draw single, multiple charts, using bin width, axis correction, changing colors, etc. The histogram helps to visualize the different shapes of the data. Finally, we have seen how the histogram allows analyzing data sets, and midpoints are used as labels of the class. The histogram helps in changing intervals to produce an enhanced description of the data and works, particularly with numeric data. histograms are more preferred in the analysis due to their advantage of displaying a large set of data. Based on the output we could visually skew the data and easy to make some assumptions.

Recommended Articles

This has been a guide on Histogram in R. Here we have discussed the basic concept, and how to create a Histogram in R with Examples. You may also look at the following articles to learn more –

  1. Histogram Examples
  2. Career in R programming
  3. Career in Computer Programming
  4. How to Create a Line Graph in R?
  5. Guide to Histogram in Matlab
Popular Course in this category R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price
9

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » R |中的直方图了解如何使用 R 软件创建直方图

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情