如何在R中创建密度图?

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

您可以使用ggplot2在R中创建密度图。
要使用ggplot2进行绘图,必须使用函数geom_density()。
让我们看看它在本教程中的工作方式。

什么是密度图?

密度图,也称为内核密度图,用于了解数据的分布。
被认为是呈现给定时间段内变量分布的有效方法。
密度图的峰值提供了一段时间内集中值的数据。

您可以在基本R中创建密度图,也可以如上所述使用ggplot创建密度图。

密度图相对于直方图的优势

  • 密度图比直方图更好,因为它们可以有效地确定分布形状。

  • 与直方图不同,密度图不受容器影响。

  • 它可以清晰地显示一段时间内的数据分布。

在R中可以绘制多种类型的密度图。
根据各种问题和要求使用所有密度图。

让我们进入主题,我们可以在R中绘制所有类型的密度图

在R中使用ggplot2的基本密度图

在本节中,我们将使用R中的ggplot2创建基本密度图。
为此,我们将导入定价数据文件。
之后,我们将绘制该文件中存在的值的密度图。

您可以从此链接下载本教程中使用的示例文件。
来源:虹膜数据集,Google Play商店应用数据集

执行以下代码以在Rstudio中创建一个简单的密度图。

library(ggplot2)               #imports ggplot2
library(dplyr)                 #imports dplyr

#loads the data from the .txt file with header true. 
data <- read.table("price.txt", header=TRUE)
 
#creating the density plot 
data %>%
+     filter( Dollars<400 ) %>%
+     ggplot( aes(x=Dollars)) +
+     geom_density(fill="#4D9DDA", color="#4D9DDA", alpha=0.8)

R中的密度图

现在,我们将尝试为密度图添加标题。
执行以下代码以创建具有合适标题的密度图。

library(ggplot2)               #imports ggplot2
library(dplyr)                 #imports dplyr
 
#loads the data from the .txt file with header true. 
data <- read.table("price.txt", header=TRUE)
 
#creating the density plot 
data %>%
+     filter( Dollars<400 ) %>%
+     ggplot( aes(x=Dollars)) +
+     geom_density(fill="#4D9DDA", color="#4D9DDA", alpha=0.8)+
+    ggtitle("State wise population distribution in the USA")+theme_ipsum()

hrbrtheme:hrbrthemes是ggplot2中的另一个主题包,主要集中于情节中的版式。

使用ggplot2在R中的镜像密度图

如您所知,密度图是值分布的表示。
镜面密度图用于比较两个不同的图。

值的完全相反或者镜像绘制将使比较非常容易和有效。
为了使用ggplot2创建此镜像密度图,我们使用geom_density函数。

要绘制反射镜密度图,请在Rstudio中执行以下代码。

library(ggplot2)       #importing library ggplot2
library(hrbrthemes)    #importing library hrbrthemes

#sample data taken for plotting
data <- data.frame(data1 = rnorm(1000),data2 = rnorm(1000, mean=2))

p <- ggplot(data, aes(x=x))+
 
#top portion plot 
geom_density( aes(x = data1, y = ..density..), fill="#D2CE12" ) +
geom_label( aes(x=6, y=0.20, label="data1"), color="#1EAEC2") +
 
#bottom portion plot
geom_density( aes(x = data2, y = -..density..), fill= "#66B32D") +
geom_label( aes(x=6, y=-0.20, label="data2"), color="#1EAEC2") +
theme_ipsum() +
xlab("x values")

使用ggplot2在R中的多个密度图

多重密度图:这些图使用多个变量和多个填充来创建图形,以显示值的分布。

在本节中,我们将使用ggplot2创建多个密度图。
在此图中,我们使用的是Kaggle中提供的Google Play商店数据。

我们正在使用"内容分级和数字"数据绘制图表。

执行以下代码以在R studio中创建多重密度图。

library(ggplot2)     #imports library ggplot2
library(hrbrthemes)  #imports library hrbrthemes
library(dplyr)       #imports the dplyr function
Library (tidyr)      #imports the tidyverse package
library(viridis)     #imports the library viridis
 
readfile<-read.csv("googleplaystore.csv")  #reads the data
 
x1 <- ggplot(googleplaystore, aes(x=Number, group=Content.Rating, fill=Content.Rating))+
geom_density(adjust=1)+         #plots the density graph
theme_ipsum()
x1

使用facet_wrap()函数创建小倍数

密度图的较小倍数将帮助我们理解每个变量的分布。
各个图将帮助我们比较位于同一轴上的不同变量分布。

为此,我们使用facet_wrap()函数。

要创建密度图的较小倍数,请在Rstudio中执行以下代码。

readfile <- read.csv("googleplaystore.csv")   #reads the file
ggplot(data=readfile, aes(x=Number, group=Content.Rating, fill=Content.Rating)) +
   geom_density(adjust=2) +
   theme_ipsum() +
   facet_wrap(~Content.Rating) +    #creates the small multiples
   theme(
       legend.position="none",
       panel.spacing = unit(0.2, "lines"),
       axis.ticks.x=element_blank()
   )

使用ggplot2在R中堆积密度图

堆积密度图是显示给定值的最频繁数据的图。
但是堆积图的缺点是它不能清楚地显示数据的分布。

其中我们使用Google Play商店数据创建堆积密度图。

执行以下代码以在R studio中创建堆积密度图。

readfile <- read.csv("googleplaystore.csv")   #reads the data
plt <- ggplot(data=readfile, aes(x=Number, group=Content.Rating, fill=Content.Rating)) +
   geom_density(adjust=1.5, position="fill") +
   theme_ipsum()
 
plt   #displays the plot

使用ggplot2在R中的2D密度图

R提供函数geom_density2d()来绘制二维密度图。
2D图形本质上在视觉上很吸引人,并且可以有效地交流见解。

为此,我们使用kaggle中提供的鸢尾花数据集。
让我们绘制出萼片长度和方差的密度图。

执行以下代码以在R studio中创建2D密度图。

library(ggplot2)

#reads the iris flower data
readfile <- read.csv("Iris.csv")
View(readfile)

#marks the x and y axis values 
x <- ggplot(data=readfile, aes(x=SepalLength.Cm, y=SpealWidth.Cm))
View(x)

#generated the 2D density plot
x+stat_density2d()+geom_point()

#create an appealing 2D plot
values+stat_density2d(aes(fill=..density..), geom='raster',contour=FALSE)
values+stat_density2d(aes(fill=..density..), geom='tile',contour=FALSE)+geom_point(color='white')

#shows the density points in the plot
densitypoints <- values+stat_density2d(aes(fill=..density..), geom='tile',contour=FALSE)+geom_point(color='white')

#creates the x and y labels 
densitypoints+xlab('Sepal length')+ylab('Sepal width')