heatmap_barplot
写在前面
【这图怎么画】系列的图都来自VIP群
里同学的提问。推文只是对图片的复现,不代表作者对图片展现形式的认同。欢迎同学们在群里分析有意思的图片。
本期图片
❝Title:Nitrogen stabilizers mitigate nitrous oxide emissions across maize production areas of China: A multi-agroecosystems evaluation
期刊:European Journal of Agronomy
❞
读图
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