密度图的密度估计_不同类型的二维密度图小教程
R相關小教程鏈接:
用R構建氣泡圖案例小教程
【小教程】散點圖、餅圖怎么在我的文章中完美展示小教程
熱圖在論文發表中完美呈現小教程
R與密度、函數、變量的微妙關系
北京市計算中心醫用數據庫建設解決方案
更多內容,請關注“生信會議”公眾號
Different types of 2d density chart.?Source
Density 2d
2d密度圖顯示了兩個數值變量之間的關系。一個在X軸上,另一個在Y軸上,就像散點圖一樣。然后,在二維空間的特定區域內觀察到的數量被計數并由顏色梯度表示。二維密度圖有幾種類型:
01
Definition
Definition
此頁面專門用于一組圖形,允許研究兩個定量變量的組合分布。這些圖形基本上是眾所周知的密度圖和直方圖的擴展。
對于每個變化,全局概念是相同的。一個變量在X軸上表示,另一個變量在Y軸上表示,就像散射圖(1)一樣)。然后,計算二維空間特定區域內的觀測數,并用顏色梯度表示。形狀可以變化:
·?六邊形經常被使用,導致一個六邊形圖表(2)
·?正方形制作2d直方圖(3)
·?計算核密度估計也可以得到2d密度圖(5)或等高線圖(6)
以下是這些不同可能性的概述
# Librariesimport?numpy as?npimport?matplotlib.pyplot as?pltfrom?scipy.stats import?kde
?# Create data: 200 points
data =?np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
x, y =?data.T
?# Create a figure with 6 plot areas
fig, axes =?plt.subplots(ncols=6, nrows=1, figsize=(21, 5))
?# Everything starts with a Scatterplot
axes[0].set_title('Scatterplot')
axes[0].plot(x, y, 'ko')# Thus we can cut the plotting window in several hexbins
nbins =?20
axes[1].set_title('Hexbin')
axes[1].hexbin(x, y, gridsize=nbins, cmap=plt.cm.BuGn_r)
?# 2D Histogram
axes[2].set_title('2D Histogram')
axes[2].hist2d(x, y, bins=nbins, cmap=plt.cm.BuGn_r)
?# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
k =?kde.gaussian_kde(data.T)
xi, yi =?np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi =?k(np.vstack([xi.flatten(), yi.flatten()]))
?# plot a density
axes[3].set_title('Calculate Gaussian KDE')
axes[3].pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.BuGn_r)
?# add shading
axes[4].set_title('2D Density with shading')
axes[4].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
?# contour
axes[5].set_title('Contour')
axes[5].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
axes[5].contour(xi, yi, zi.reshape(xi.shape) )# save
plt.savefig("IMG/density2d.png")
02
What for
What for
二維分布是非常有用的,以避免過度繪制在一個散射圖。這里有一個例子,顯示了超圖散點圖和2d密度圖之間的區別。在第二種情況下,出現了一個非常明顯的隱藏模式:
# Librarieslibrary(tidyverse)library(hrbrthemes)library(viridis)library(patchwork)
# Dataset:
a
b
c
data
p1 %??ggplot( aes(x=x, y=y)) +????geom_point(color="#69b3a2", size=2) +????theme_ipsum() +????theme(
??????legend.position="none"
????)
p2
????legend.position='none'
??)
p1 +?p2
03
Variation
Variation
2d發行是一種罕見的值得使用3d的情況。
可以在網格中轉換散點圖信息,并計算網格每個位置上的數據點的數量。然后,不是用漸變顏色來表示這個數字,表面圖使用3d來表示密度比其他的要高。
在這種情況下,3組的位置變得明顯:
library(plotly)library(MASS)
# Compute kde2d
kd
# Plot with plotlyplot_ly(x =?kd$x, y =?kd$y, z =?kd$z) %>%?add_surface()
03
Variation
Variation
2d發行是一種罕見的值得使用3d的情況。
可以在網格中轉換散點圖信息,并計算網格每個位置上的數據點的數量。然后,不是用漸變顏色來表示這個數字,表面圖使用3d來表示密度比其他的要高。
在這種情況下,3組的位置變得明顯:
library(plotly)library(MASS)
# Compute kde2d
kd
# Plot with plotlyplot_ly(x =?kd$x, y =?kd$y, z =?kd$z) %>%?add_surface()
一、
2D HISTOGRAM WITH?GEOM_BIN2D()
這是經典直方圖的二維版本。地塊區域被分割成許多小正方形,每個正方形中的點數由其顏色表示。
2d density plot with ggplot2
這篇文章介紹了2d密度圖的概念,并解釋了如何使用R和ggplot2來構建它。考慮了二維直方圖、hexbin圖、二維分布等。
The issue with?geom_point()
如果你有大量的點,一個2d密度圖對于研究兩個數值變量之間的關系是有用的。
為了避免重疊(就像旁邊的散點圖一樣),它將地塊區域劃分為大量的小片段,并表示該片段中的點數。
二維密度圖有幾種類型。每個都有自己的ggplot2功能。這篇文章描述了所有這些。
# Librarylibrary(tidyverse) # Dataa
2d Histogramwith?geom_bin2d()
對于2d直方圖,plot area被劃分為多個正方形。(這是一個經典的直方圖的2d版本)。使用geom_bin_2d()函數調用它。此函數提供了一個bin參數,用于控制要顯示的bin的數量。
注意:如果您不相信垃圾箱選項的重要性,請閱讀本文。
# 2d histogram with default optionggplot(data, aes(x=x, y=y) ) +??geom_bin2d() +??theme_bw() # Bin size control + color paletteggplot(data, aes(x=x, y=y) ) +??geom_bin2d(bins =?70) +??scale_fill_continuous(type =?"viridis") +??theme_bw()
Hexbin chart with?geom_hex()
另一種方法是將地塊劃分為多個六邊形:因此稱為hexbin圖,使用geom_hex()函數制作。
這個函數還提供了bin參數,用于控制每個軸的除數。
# Hexbin chart with default optionggplot(data, aes(x=x, y=y) ) +??geom_hex() +??theme_bw() # Bin size control + color paletteggplot(data, aes(x=x, y=y) ) +??geom_hex(bins =?70) +??scale_fill_continuous(type =?"viridis") +??theme_bw()
2d distribution with?geom_density_2d?or?stat_density_2d
由于可以繪制密度圖而不是柱狀圖,因此可以計算2d密度并表示它。ggplot2提供了幾種可能性:您可以顯示分布或區域的輪廓線,或使用光柵函數:
# Show the contour onlyggplot(data, aes(x=x, y=y) ) +??geom_density_2d() # Show the area onlyggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..level..), geom =?"polygon") # Area + contourggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..level..), geom =?"polygon", colour="white") # Using rasterggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..density..), geom =?"raster", contour =?FALSE) +??scale_x_continuous(expand =?c(0, 0)) +??scale_y_continuous(expand =?c(0, 0)) +??theme( ???legend.position='none'??)
Customize the color palette
無論你使用的是2d柱狀圖、hexbin圖還是2d分布,你都可以并且應該自定義圖表的顏色。這里有一個使用scale_fill_distiller()函數的建議。您可以在圖庫的ggplot2部分中看到其他方法。
# Call the palette with a numberggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..density..), geom =?"raster", contour =?FALSE) +??scale_fill_distiller(palette=4, direction=-1) +??scale_x_continuous(expand =?c(0, 0)) +??scale_y_continuous(expand =?c(0, 0)) +??theme( ???legend.position='none'??) # The direction argument allows to reverse the paletteggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..density..), geom =?"raster", contour =?FALSE) +??scale_fill_distiller(palette=4, direction=1) +??scale_x_continuous(expand =?c(0, 0)) +??scale_y_continuous(expand =?c(0, 0)) +??theme( ???legend.position='none'??) # You can also call the palette using a name.ggplot(data, aes(x=x, y=y) ) +??stat_density_2d(aes(fill =?..density..), geom =?"raster", contour =?FALSE) +??scale_fill_distiller(palette=?"Spectral", direction=1) +??scale_x_continuous(expand =?c(0, 0)) +??scale_y_continuous(expand =?c(0, 0)) +??theme( ???legend.position='none'??)
二、
OTHER EXAMPLES
Hexbin chart with the hexbin package
這篇文章解釋了如何使用hexbin包用R構建hexbin圖。Hexbin圖是一個2d密度圖,允許可視化兩個數值變量之間的關系。
在顯示大型數據集時,散點圖很難解釋,因為點不可避免地會覆蓋圖,而且可以??不能單獨區分。
bin可以被看作是一個二維的直方圖,其中容器的陰影代替了條形的高度。這種技術是在hexbin包中計算的。
這個例子已經由Myles Harrison發表在r -blogger上。
# Packageslibrary(hexbin)library(RColorBrewer) # Create datax
Hexbin chart and scatterplot with ggplot2
這篇文章解釋了如何使用R和ggplot2構建一個頂部帶有散點圖的hexbin圖。它是對使用ggplot2的2d密度圖頁面的一個補充。
此圖擴展了使用ggplot2文檔的2d密度圖中描述的概念。它簡單地說明了可以在二維密度圖的頂部添加散點圖。
# librarylibrary(ggplot2) # datasample_data
培訓最新安排:
2020.10.19-23實用生物信息學研討班
2020.10.27-30生物醫學公共數據深度挖掘及應用培訓班
2020.11.03-06轉錄組學專題實操班
2020.11.11-13生物分子互作常用軟件實操班
2020.11.18-20微生物組學數據分析與挖掘專題培訓班
2020.11.24-27生物信息學Python語言實操班
2020.11.25-27?10X Genomics單細胞轉錄組測序及多組學數據挖掘技術培訓班
2020.12.02-04基因組關聯分析技術應用培訓班
2020.12-09-11生命科學的數據可視化與科研作圖—實用工具與技巧實操班
2020.12.16-18多組學數據分析及挖掘培訓班
2020.12.21-23計算機輔助設計—分子模擬與蛋白互作研討班
2020.12.28-31數據分析與R語言制圖實操班
【咨詢請聯系】
QQ號:2814500767
郵箱:bcc-sxpx@bcc.ac.cn
徐老師?010-59341786,15801436028(微信同號)
員老師?010-59341773,18701529461(微信同號)
技術服務
數據庫構建
可提10X空間轉錄組
10X單細胞轉錄組
有參(無參)轉錄組
16S
基因關聯分析
蛋白分子對接
同源建模
分子動力學
網絡藥理學
虛擬篩選等等相關技術服務、以及對應的培訓學習
相關業務咨詢
王老師:15001065280
郵箱:wangxf@bcc.ac.cn
總結
以上是生活随笔為你收集整理的密度图的密度估计_不同类型的二维密度图小教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python字符串相加_Python实用
- 下一篇: python的数据库_python数据库