# 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.

## 什么是密度图？ (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.

## 密度图相对于直方图的优势 (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.

Let’s roll into the topics and we can plot all types of density plots in 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.

Sources: Iris dataset, Google play store apps dataset

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


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

#creating the density plot
data %>%
+     filter( Dollars<400 ) %>%
+     ggplot( aes(x=Dollars)) +
+     geom_density(fill="#4D9DDA", color="#4D9DDA", alpha=0.8)



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

#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 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.

To plot the mirror density plot, execute the below code in 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中的多个密度图 (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.

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.


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

x1 <- ggplot(googleplaystore, aes(x=Number, group=Content.Rating, fill=Content.Rating))+
theme_ipsum()
x1


## 使用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.

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


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中的堆积密度图 (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.

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


plt <- ggplot(data=readfile, aes(x=Number, group=Content.Rating, fill=Content.Rating)) +
theme_ipsum()

plt   #displays the plot


## 使用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.

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


library(ggplot2)

#marks the x and y axis values
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.

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

## 结论 (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教程。 保持学习！！！

r语言中 密度图

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

12-31 3439

07-01 3519
12-23 1万+
02-17 1267
07-15 3643
02-16 6271
03-24 2万+
01-11 1万+
08-12 2万+
10-26 2万+
10-06 5617
08-27 7513