r语言中 密度图_如何在R中创建密度图?

r语言中 密度图

You can create a density plot in R using ggplot2. For plotting using ggplot2, you have to use the function geom_density(). Let’s see how it works in this tutorial.

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

什么是密度图? (What is a Density plot?)

Density plots, also known as Kernel density plots, they’re are used to understand the distribution of data. It is considered as an effective way to present the variable distribution over the given time period. The density plot’s peak gives the data of concentrated values over the time period.

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

You can either create a density plot in basic R or by using ggplot as mentioned above.

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



密度图相对于直方图的优势 (Advantages of Density plots over Histograms)

  • Density plots are better than histograms, as they can determine the distribution shape effectively.

    密度图比直方图更好,因为它们可以有效地确定分布形状。
  • Unlike histograms, density plots are not affected by bins.

    与直方图不同,密度图不受容器影响。
  • It gives a clear visualization of the data distribution over a time period.

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

There are many types of density plots are available to plot in R. All of them are used based on various problems and requirements.

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

Let’s roll into the topics and we can plot all types of density plots in R

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



在R中使用ggplot2的基本密度图 (Basic density plot using ggplot2 in R)

In this section we are creating a basic density plot using ggplot2 in R. For this purpose, we will import a pricing data file. After that, we will plot the density plot for the values present in that file.

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

You can download the sample files used in this tutorial from this link.
Sources: Iris dataset, Google play store apps dataset

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

Execute the below code to create a simple density plot in Rstudio.

执行以下代码以在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)

Density Plot in R
Density Plot in R
R中的密度图

Now we will try to add a title to our density plot. Execute the below code to create a density plot with a suitable title.

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


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()

density plot in r

hrbrtheme: hrbrthemes is an additional theme package in ggplot2 which mainly concentrates on typography in the plots.

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



使用ggplot2在R中的镜像密度图 (The Mirror density plots in R using ggplot2)

As you know that the density plots are the representation of the distribution of the values. The mirror density plots are used to compare the 2 different plots.

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

The exactly opposite or mirror plot of the values will make comparison very easy and efficient. For creating this mirror density plot using ggplot2, we use geom_density function.

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

To plot the mirror density plot, execute the below code in Rstudio.

要绘制反射镜密度图,请在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")

mirror density plot in r


使用ggplot2在R中的多个密度图 (Multiple Density plots in R using ggplot2)

Multiple density plots: These are the plots that use multiple variables and multiple fills to create a graph, which shows the distribution of values.

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

In this section, we are going to create multiple density plots using ggplot2. In this plot, we are using the google play store data which is available in Kaggle.

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

We are plotting the graph using the Content Rating and Numbers data.

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

Execute the below code to create the multiple density plot in R studio.

执行以下代码以在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
density plot in r


使用facet_wrap()函数创建小倍数 (Creating Small Multiples using facet_wrap() function )

This small multiples of the density plot will help us to understand the distribution of each variable. The individual plots will help us to compare the different variable distribution as they lie on the same axis.

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

For this purpose we are using facet_wrap() function.

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

To create the small multiples of the density plot, execute the below code in Rstudio.

要创建密度图的较小倍数,请在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()
     )
multiple density plots in r


使用ggplot2在R中的堆积密度图 (Stacked density plots in R using ggplot2)

The stacking density plot is the plot which shows the most frequent data for the given value. But the disadvantage of the stacked plot is that it does not clearly show the distribution of the data.

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

Here we are creating a stacked density plot using the google play store data.

在这里,我们使用Google Play商店数据创建堆积密度图。

Execute the below code to create the stacked density plot in R studio.

执行以下代码以在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
stacked density plot in R


使用ggplot2在R中的2D密度图 (2D Density plots in R using ggplot2)

R offers the function geom_density2d() to plot the two dimensional density plots. 2D graphs are visually appealing in nature and can communiacte the insights in an effective manner .

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

For this purpose we are using the iris flower dataset which is available in the kaggle webiste. Lets plot the density plot for sepal length and with varibales.

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

Execute the below code to create a 2D density plot in R studio.

执行以下代码以在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')



The iris flower data is shown below.

鸢尾花数据如下所示。

iris flower data

The 2-Dimensional density plot of the values present in iris flower data.

鸢尾花数据中存在的值的二维密度图。

2d density plot in r


结论 (Conclusion)

R is one of the best visualization heavy language. In this tutorial, we have gone through the density plots, its benefits and we have plotted the density plots using ggplot2.

R是最好的可视化繁重语言之一。 在本教程中,我们详细介绍了密度图及其优点,并使用ggplot2绘制了密度图。

There are many types of density plots in R. We have plotted basic density plots with x and y labels, mirror density plots, multi-density plots, stacked density plots and finally we have plotted the 2-dimensional density plot using the iris dataset.

R中有很多类型的密度图。我们已经绘制了带有x和y标签的 基本密度图镜面密度图多密度图,堆积密度图 ,最后我们使用虹膜数据集绘制了二维密度图

R offers great libraries and functions to create visually appealing graphs for any purpose. That’s all for now. Learn more…Practice more and stay connected for more R tutorials. Keep learning!!!

R提供了出色的库和函数 ,可以为任何目的创建吸引人的图形 。 目前为止就这样了。 了解更多…更多实践,并保持联系以获取更多R教程。 保持学习!!!

翻译自: https://www.journaldev.com/38367/density-plot-in-r

r语言中 密度图

  • 0
    点赞
  • 0
    评论
  • 12
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值