R语言绘图包系列:
- R语言绘图包01--优秀的拼图包patchwork
- R语言绘图包02--热图pheatmap
- R语言绘图包03--火山图EnhancedVolcano
- R语言绘图包04--GOplot:富集分析结果可视化
- R语言绘图包05--韦恩图的绘制:ggvenn和VennDiagram
- R语言绘图包06--基因表达相关性绘图corrplot
- R语言绘图包07--多集合可视化UpSetR
- R语言绘图包08--森林图的绘制:forestplot
- R语言绘图包09--序列分析图的绘制ggseqlogo
- R语言绘图包10--Circos图的绘制:circlize包
- R语言绘图包11--森林图的绘制(2):forestploter包
1. hexin包
# Packages
library(hexbin)
library(RColorBrewer)
# Create data
x <- rnorm(mean=1.5, 5000)
y <- rnorm(mean=1.6, 5000)
# Make the plot
bin<-hexbin(x, y, xbins=40)
my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))
plot(bin, main="" , colramp=my_colors , legend=F )
2. ggplot2 (geom_hex)
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex()
Few bins
You can control de number of bins in both vertical and horizontal directions. Default value is 30.
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex(bins = 15)
Too many bins
Note that if you set too many bins the hexbin chart will look like a classical scatter plot.
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex(bins = 60)
Border color
The border for all hexagons can be customized with the color
argument of the geom_hex
function.
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex(color = "white")
Fill and transparency
You can also set a fill color for all the hexagons with fill
and control the transparency of the colors with alpha
.
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex(color = 1, fill = 4, alpha = 0.4)
Color palette
If you want to keep the gradient color palette representing values you can change the default colors with scale_fill_viridis_c
, scale_fill_gradient
or a similar function.
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
scale_fill_viridis_c()
Legend customization
Width and height
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
guides(fill = guide_colourbar(title = "Count"))
Remove the labels and the ticks
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
guides(fill = guide_colourbar(label = FALSE,
ticks = FALSE))
Remove the legend
# install.packages("ggplot2")
library(ggplot2)
# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
ggplot(df, aes(x = x, y = y)) +
geom_hex() +
theme(legend.position = "none")
参考: