这图怎么画| 相关性热图+柱状图

简介: 这图怎么画| 相关性热图+柱状图

heatmap_barplot

写在前面

【这图怎么画】系列的图都来自VIP群里同学的提问。推文只是对图片的复现,不代表作者对图片展现形式的认同。欢迎同学们在群里分析有意思的图片。

本期图片

Title:Nitrogen stabilizers mitigate nitrous oxide emissions across maize  production areas of China: A multi-agroecosystems evaluation

期刊:European Journal of Agronomy

Doi:https://doi.org/10.1016/j.eja.2022.126692

读图

fig

热图展示的是相关分析随机森林回归模型的结果。圆圈大小代表变量重要性( variable importance),颜色代表 Pearson’s correlation coefficients。图难度较小,只需注意一些小细节即可。

复现结果

result


绘图

# data pre
df_cor <- matrix(runif(60,-1,1),
                 nrow=10, 
                 ncol=6,
                 dimnames=list( paste0('indictator',1:10),
                                paste0('sample',1:6)))
df_cor <- data.frame(df_cor)
head(df_cor)
df_cor$indicator <- row.names(df_cor)
library(tidyr)
df_cor_long <- gather(df_cor, sample, value,-indicator)
head(df_cor_long)
df_imp <- matrix(runif(60,0,15),
                 nrow=10, 
                 ncol=6,
                 dimnames=list( paste0('indictator',1:10),
                                paste0('sample',1:6)))
df_imp <- data.frame(df_imp)
head(df_imp)
df_imp$indicator <- row.names(df_imp)
library(tidyr)
df_imp_long <- gather(df_imp, sample, value,-indicator)
head(df_imp_long)
df_var <- data.frame(sample = paste0('sample',1:6),
                     var =round( runif(6,0,100),0) )
head(df_var)
# plot heatmap
library(ggplot2)
p1 <- ggplot()+
  geom_tile(data = df_cor_long,
            mapping = aes(sample,indicator,fill = value))+
  scale_fill_gradient2(name = 'Correlation',
                       limit = c(-1.001,1.001),
                       breaks = c(-1.0,-0.5,0.0,0.5,1.0),
                       low = '#2ab49b',
                       mid = 'white',
                       high = '#ea7f58')+
  geom_point(data = df_imp_long,
             mapping = aes(sample,indicator,size = value),
             shape = 1,
             stroke = 0.6,
             color = 'black')+
  scale_size_continuous(name = 'Importance(%)',
                        limit = c(-0.001,15.1),
                        breaks = c(0,5,10,15))+
  scale_y_discrete(expand = c(0,0))+ 
  scale_x_discrete(expand = c(0,0))+ 
  theme_bw()+
  xlab(NULL) + 
  ylab(NULL)+
  theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"))
p1
# plot barplot
p2 <- ggplot(df_var,aes(sample,var))+
  geom_bar(stat = 'identity',
           fill = '#2d89bf')+
  xlab(NULL) + 
  ylab('Exp var(%)')+
  theme_bw()+
  theme(panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        axis.text.x = element_blank())
p2
# patch
library(patchwork)
p2/p1+plot_layout(ncol = 1,
                  heights = c(0.8, 2))
ggsave('heatmap.pdf',width = 6,height = 6)

result

相关文章
|
8月前
|
数据挖掘
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
233 0
|
8月前
|
数据挖掘
这图怎么画| 有点复杂的散点图
这图怎么画| 有点复杂的散点图
33 0
|
8月前
|
人工智能 数据挖掘
这图怎么画| 气泡热图(基因表达泛癌分析)
这图怎么画| 气泡热图(基因表达泛癌分析)
83 0
|
9月前
|
数据处理
第8章 概率统计——8.5 统计作图
第8章 概率统计——8.5 统计作图
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
1255 0
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
1月前
R语言中绘制箱形图的替代品:蜂群图和小提琴图
R语言中绘制箱形图的替代品:蜂群图和小提琴图
|
8月前
|
数据挖掘 数据处理
这图怎么画| 还是热图(免疫治疗反应预测)
这图怎么画| 还是热图(免疫治疗反应预测)
43 0
|
8月前
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
183 0
|
8月前
跟着 Cell 学作图 | 柱状图+误差棒+蜂群图
跟着 Cell 学作图 | 柱状图+误差棒+蜂群图
110 0
|
8月前
|
数据挖掘
这图怎么画| 多组箱线图+组间/内差异分析
这图怎么画| 多组箱线图+组间/内差异分析
123 0