r语⾔绘制直⽅图_如何在R中绘制直⽅图
r语⾔ 绘制直⽅图
A histogram is a graphical reprentation of the values
In this tutorial, we’ll go over the steps to plot a histogram in R. A histogram
along with its range. It is similar to a bar plot and each bar prent in a histogram will reprent the range and height of the specified value.
直⽅图是值及其范围的图形表⽰。 它类似于条形图,直⽅图中的每个条形都将表⽰指在本教程中,我们将介绍在R中绘制直⽅图的步骤。 直⽅图
定值的范围和⾼度。
R offers standard function hist() to plot the histogram in Rstudio. It also offers function geom_density() to plot histogram using ggplot2.
服务生
R提供标准函数hist()来绘制Rstudio中的直⽅图。 它还提供了函数geom_density()来使⽤ggplot2绘制直⽅图。
制取氧气直⽅图的优点 (Advantages of Histograms)
A histogram provides the distribution of the data, frequency of the data along with its range.
直⽅图提供数据的分布,数据的频率及其范围。
It is an easier way to visualize large data ts.
这是⼀种可视化⼤型数据集的简便⽅法。
The histogram also shows the skewness of the data.
直⽅图还显⽰了数据的偏度 。
R中直⽅图的类型 (Types of Histogram plots in R)
Bad on the distribution of the data, a histogram exhibits many different shapes. In this ction, we will try to understand the different types of histogram shapes and their meaning.
根据数据的分布,直⽅图显⽰许多不同的形状。 在本节中,我们将尝试了解直⽅图形状的不同类型及其含义。
The major types of histogram distributions are,
直⽅图分布的主要类型是
Normal distribution.
正态分布。
Right skewed distribution.
右偏分布。
Left skewed distribution.
左偏分布。
Bimodal distribution
双峰分布
R中的基本直⽅图 (Basic Histogram in R)
In this ction, we will plot a simple histogram using the ‘airquality’ data t.
在本节中,我们将使⽤“空⽓质量”数据集绘制⼀个简单的直⽅图。
Execute the below code to plot this simple histogram.
执⾏以下代码以绘制此简单直⽅图。
#this code imports the datat from the R(built-in data ts)
datats::airquality
#creates the simple histogram
汽车厂实习报告hist(airquality$Temp, xlab = 'Temparature', ylab='Frequency', main='Simple histogram plot', col = 'yellow', border = 'black')
Histogram Temperature
直⽅图温度
正态分布 (Normal distribution)
A normal distribution in the histogram is the ideal bell-shaped plot, which contains less or no random data.在直⽅图正态分布是钟形的理想曲线图,它包含较少或不随机数据。
This distribution shows that the majority of the values are concentrated at the center range.
此分布表明⼤多数值集中在中⼼范围。
However, the remaining data points will end up as a tail in both sides as you can e in the below plot.
但是,剩余的数据点将在两侧都变成尾巴,如下图所⽰。
Execute the below code to create the histogram which shows the normal distribution.
执⾏以下代码以创建显⽰正态分布的直⽅图。
#imports the default datat which is prent 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 tosa
2 4.9 3.0 1.4 0.2 tosa
3 4.7 3.2 1.3 0.2 tosa
4 4.6 3.1 1.
识字教学5 0.2 tosa
5 5.0 3.
6 1.4 0.2 tosa
#creates the histogram bins bad on 'pal length'
hist(iris$Sepal.Width, xlab = 'Sepal width', ylab = 'frequency', main='normal distribution of the data', col = 'brown')
R中的左或负偏直⽅图 (Left or Negatively Skewed Histogram in R)
In this ction, we will plot the left or negetive skewed histogram.
在本节中,我们将绘制左或消极的偏斜直⽅图。
Negative skewed: If the histogram distribution shows the values which are concentrated on the right side and the tail will be on the left side or on the negative value side, then it is called as negatively of left-skewed distribution.
负偏斜 :如果直⽅图分布显⽰的值集中在右侧,⽽尾巴将在左侧或负值侧 ,则称为负偏斜分布。
Execute the below code to create a negetive skewed histogram in Rstudio.
执⾏以下代码,以在Rstudio中创建⼀个具有偏差的直⽅图。
Datat:
数据集:
#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')媒体邀请函
直⽅图或正偏图 (Right or Positively skewed Histogram )
In this ction, we will plot the right or positively skewed histogram.
在本节中,我们将绘制右或正偏直⽅图。
Positive skewed: If the histogram’s distribution shows that the values are concentrated on the left side and tail is on the right side of the plot, then such distribution is called positively or right-skewed histogram distribution.
正偏斜:如果直⽅图的分布显⽰值集中在图的左侧,⽽尾部位于图的右侧 ,则这种分布称为正偏斜或右偏直⽅图分布。
Execute the below code to plot the right or positively skewed histogram.
执⾏以下代码以绘制右或正偏直⽅图。
#imports the data from the R's default datat named 'attenu'.
datats::attenu
#plots the right or posiively skewed distribution
hist(attenu$accel, xlab = 'attenu', ylab = 'Frequency', main = 'Right or positively skewed distribution', col = 'brown')
使⽤直⽅图绘制的数据的双峰分布 (Bimodal Distribution of the data plotted using Histogram) In this ction, we will plot a bimodal distribution of the data.
在本节中,我们将绘制数据的双峰分布。
Bimodal distribution: Bimodal distribution is a type of histogram distribution, where you can witness two data peaks.
双峰分布:双峰分布是直⽅图分布的⼀种,您可以在其中看到两个数据峰 。
In the below graph, the x value ‘quakes’ reprent the quakes data distribution.
在下图中, x值“ quakes”表⽰地震数据分布。
Execute the below code to plot the bimodal distribution.
执⾏以下代码以绘制双峰分布。
#imports the data from the R's default datat named 'quakes'
datats::quakes
#plots the bimodal histogram distribution
hist(quakes$depth, xlab = 'Quakes', ylab = 'Frequency', main = 'Bimodal distribution', col = 'brown')
在R中使⽤ggplot2绘制直⽅图。 (Plotting a Histogram using ggplot2 in R.)
As you know is the most ud visualization package plot2 offers great themes and functions to create visually
appealing graphs.
如您所知是中最常⽤的可视化软件包。ggplot2提供了出⾊的主题和功能来创建吸引⼈的图形 。
In this ction, we will plot the histogram of the values prent in the ‘diamonds’ data t, which is prent in R by
default.
在本节中,我们将绘制“钻⽯”数据集中存在的值的直⽅图,该数据默认情况下在R中存在。
Execute the below code to plot the histogram using ggplot2.
执⾏以下代码,使⽤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