Different types of 2d density chart. Source
2d密度圖顯示了兩個數值變量之間的關係。一個在X軸上,另一個在Y軸上,就像散點圖一樣。然後,在二維空間的特定區域內觀察到的數量被計數並由顏色梯度表示。二維密度圖有幾種類型:
此頁面專門用於一組圖形,允許研究兩個定量變量的組合分布。這些圖形基本上是眾所周知的密度圖和直方圖的擴展。
對於每個變化,全局概念是相同的。一個變量在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")
二維分布是非常有用的,以避免過度繪製在一個散射圖。這裡有一個例子,顯示了超圖散點圖和2d密度圖之間的區別。在第二種情況下,出現了一個非常明顯的隱藏模式:
# Librarieslibrary(tidyverse)library(hrbrthemes)library(viridis)library(patchwork)
# Dataset:
a <- data.frame( x=rnorm(20000, 10, 1.2), y=rnorm(20000, 10, 1.2), group=rep("A",20000))
b <- data.frame( x=rnorm(20000, 14.5, 1.2), y=rnorm(20000, 14.5, 1.2), group=rep("B",20000))
c <- data.frame( x=rnorm(20000, 9.5, 1.5), y=rnorm(20000, 15.5, 1.5), group=rep("C",20000))
data <- do.call(rbind, list(a,b,c))
p1 <- data %>% ggplot( aes(x=x, y=y)) + geom_point(color="#69b3a2", size=2) + theme_ipsum() + theme(
legend.position="none"
)
p2 <- ggplot(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)) + scale_fill_viridis() + theme(
legend.position='none'
)
p1 + p2
2d發行是一種罕見的值得使用3d的情況。
可以在網格中轉換散點圖信息,並計算網格每個位置上的數據點的數量。然後,不是用漸變顏色來表示這個數字,表面圖使用3d來表示密度比其他的要高。
在這種情況下,3組的位置變得明顯:
library(plotly)library(MASS)
# Compute kde2d
kd <- with(data, MASS::kde2d(x, y, n = 50))
# Plot with plotlyplot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()
2d發行是一種罕見的值得使用3d的情況。
可以在網格中轉換散點圖信息,並計算網格每個位置上的數據點的數量。然後,不是用漸變顏色來表示這個數字,表面圖使用3d來表示密度比其他的要高。
在這種情況下,3組的位置變得明顯:
library(plotly)library(MASS)
# Compute kde2d
kd <- with(data, MASS::kde2d(x, y, n = 50))
# 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 <- data.frame( x=rnorm(20000, 10, 1.9), y=rnorm(20000, 10, 1.2) )b <- data.frame( x=rnorm(20000, 14.5, 1.9), y=rnorm(20000, 14.5, 1.9) )c <- data.frame( x=rnorm(20000, 9.5, 1.9), y=rnorm(20000, 15.5, 1.9) )data <- rbind(a,b,c) # Basic scatterplotggplot(data, aes(x=x, y=y) ) + geom_point()
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' )
Hexbin chart with the hexbin package
這篇文章解釋了如何使用hexbin包用R構建hexbin圖。Hexbin圖是一個2d密度圖,允許可視化兩個數值變量之間的關係。
在顯示大型數據集時,散點圖很難解釋,因為點不可避免地會覆蓋圖,而且可以??不能單獨區分。
bin可以被看作是一個二維的直方圖,其中容器的陰影代替了條形的高度。這種技術是在hexbin包中計算的。
這個例子已經由Myles Harrison發表在r -blogger上。
# Packageslibrary(hexbin)library(RColorBrewer) # Create datax <- rnorm(mean=1.5, 5000)y <- rnorm(mean=1.6, 5000) # Make the plotbin<-hexbin(x, y, xbins=40)my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))plot(bin, main="" , colramp=my_colors , legend=F )
Hexbin chart and scatterplot with ggplot2
這篇文章解釋了如何使用R和ggplot2構建一個頂部帶有散點圖的hexbin圖。它是對使用ggplot2的2d密度圖頁面的一個補充。
此圖擴展了使用ggplot2文檔的2d密度圖中描述的概念。它簡單地說明了可以在二維密度圖的頂部添加散點圖。
# librarylibrary(ggplot2) # datasample_data <- data.frame(x_values = 1:100 + rnorm(100,sd=20), y_values = 1:100 + rnorm(100,sd=27)) #plotggplot(sample_data, aes(x_values, y_values)) + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) + geom_point(colour = "white")
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