如何在R中绘制直方图

时间:2020-02-23 14:43:50  来源:igfitidea点击:

在本教程中,我们将介绍在R中绘制直方图的步骤。
直方图是值及其范围的图形表示。
它类似于条形图,直方图中的每个条形都将表示指定值的范围和高度。

R提供标准函数hist()来绘制Rstudio中的直方图。
它还提供了函数geom_density()来使用ggplot2绘制直方图。

直方图的优点

  • 直方图提供数据的分布,数据的频率及其范围。

  • 这是一种可视化大型数据集的简便方法。

  • 直方图还显示了数据的偏度。

R中直方图的类型

根据数据的分布,直方图显示许多不同的形状。
在本节中,我们将尝试了解直方图形状的不同类型及其含义。

直方图分布的主要类型是

  • 正态分布。

  • 右偏分布。

  • 左偏分布。

  • 双峰分布

R中的基本直方图

在本节中,我们将使用"空气质量"数据集绘制一个简单的直方图。

执行以下代码以绘制此简单直方图。

#this code imports the dataset from the R(built-in data sets)
datasets::airquality

#creates the simple histogram
hist(airquality$Temp, xlab = 'Temparature', ylab='Frequency', main='Simple histogram plot', col = 'yellow', border = 'black')

正态分布

直方图中的正态分布是理想的钟形图,其中包含很少或者没有随机数据。

此分布表明大多数值集中在中心范围。

但是,剩余的数据点将在两侧都变成尾巴,如下图所示。

执行以下代码以创建显示正态分布的直方图。

#imports the default dataset which is present in R
data("iris")

#reads the data
head(iris, 5)
 Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa

#creates the histogram bins based on 'sepal length'
hist(iris$Sepal.Width, xlab = 'Sepal width', ylab = 'frequency', main='normal distribution of the data', col = 'brown')

R中的左或者负偏直方图

在本节中,我们将绘制左或者负的偏斜直方图。

负偏斜:如果直方图分布显示的值集中在右侧,而尾巴将在左侧或者负值侧,则称为负偏斜分布。

执行以下代码,以在Rstudio中创建一个具有偏差的直方图。

数据集:Google Play Store数据集by kaggle

#imports the csv file 
df<- read.csv("googleplaystore.csv")
#reads the data
df
#plots the histogram which is negetively or left skewed
hist(df$Rating, xlab = 'Ratings', ylab = 'Frequency', main = 'Negetive or left skewed distribution', col='brown')

正确或者正偏直方图

在本节中,我们将绘制右或者正偏直方图。

正偏斜:如果直方图的分布表明值集中在图的左侧,而尾部位于图的右侧,则这种分布称为正偏斜或者右偏直方图分布。

执行以下代码以绘制右或者正偏直方图。

#imports the data from the R's default dataset named 'attenu'.
datasets::attenu

#plots the right or posiively skewed distribution
hist(attenu$accel, xlab = 'attenu', ylab = 'Frequency', main = 'Right or positively skewed distribution', col = 'brown')

使用直方图绘制的数据的双峰分布

在本节中,我们将绘制数据的双峰分布。

双峰分布:双峰分布是一种直方图分布,您可以其中看到两个数据峰。

在下图中,x值" quakes"表示地震数据分布。

执行以下代码以绘制双峰分布。

#imports the data from the R's default dataset named 'quakes'
datasets::quakes

#plots the bimodal histogram distribution
hist(quakes$depth, xlab = 'Quakes', ylab = 'Frequency', main = 'Bimodal distribution', col = 'brown')

在R中使用ggplot2绘制直方图。

如您所知,ggplot2是R.中最常用的可视化软件包。
ggplot2提供了出色的主题和功能来创建吸引人的图形。

在本节中,我们将绘制"钻石"数据集中存在的值的直方图,默认情况下在R中存在该值。

执行以下代码,使用ggplot2绘制直方图。

#install the required packages
install.packages('ggplot2')
install.packages('dplyr')
install.packages('ggthemes')

#import the required libraries
library(ggplot2)
library(dplyr)
library(ggthemes)

#shows the data
head(diamonds)
#plots the histogram
ggplot(diamonds, aes(carat))+geom_histogram()
#changes the bin width
ggplot(diamonds, aes(carat))+geom_histogram(binwidth = 0.01)
#adds the fill element and x,y and main labels of the graph
ggplot(diamonds, aes(carat, fill=cut))+geom_histogram()+labs(x='carats', y=' Frequency of carats')+ggtitle("Distribution of diamonds's carat by cut values")
#chnages the theme for attractive graph
ggplot(diamonds, aes(carat, fill=cut))+geom_histogram()+labs(x='carats', y=' Frequency of carats')+ggtitle("Distribution of diamonds's carat by cut values")+theme_classic()