其他
R绘图模板——热图和柱状堆积图组合图!!!
点击上方
“科研后花园”
关注我们
图片灵感来源
复现代码如下:
1、设置工作环境并加载所需R包:
rm(list=ls())#clear Global Environment
setwd('D:/桌面/热图+柱状堆积图')#设置工作路径
#加载包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(aplot) # Decorate a 'ggplot' with Associated Information
2、加载数据(根据原文编写数据,无实际意义):
#加载数据
df <- read.table("data.txt",header = 1,check.names = F)
df$group <- factor(df$group,levels = c("F","CK"))
df$species <- factor(df$species,levels = c("Pedobacter","Aridibacter","Devosia","Rhizobium",
"Phenylobacterium","Arthrobacter","Bradvrhizobium",
"Pseudomonas","Gemmatimonas","Sphingomonas"))
3、绘图:
##绘制热图
#设置渐变色
col <- colorRampPalette(c("#0066b2","#fdbd10","#ec1c24"))(50) #设置渐变色
p1 <- ggplot(df,aes(group,species,fill=value))+
#绘制热图
geom_tile(color="black")+
#添加数值
geom_text(aes(label = value), color = 'white', size = 5)+
#标题
labs(x=NULL,y=NULL,fill=NULL)+
#颜色
scale_fill_gradientn(colours = col)+
#主题
theme_void()+
theme(axis.text.x = element_text(color = "black",size=12),
axis.text.y = element_text(color = "black",size=12,hjust = 1),
legend.position = "right")#去除图例
p1
##绘制柱状堆积图
p2 <- ggplot(df,aes(species,value,fill=group))+
#柱状堆积图
geom_col()+
coord_flip()+
#标题
labs(x=NULL,y=NULL,fill=NULL)+
#颜色
scale_fill_manual(values = c("#f0b240","#62d7f6"))+
#主题
theme_classic()+
theme(axis.text.x = element_text(color = "black",size=12),
axis.text.y = element_blank(),
axis.line.x = element_line(color = "black",linewidth = 0.8),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.length.x = unit(-0.15, "cm"),
axis.ticks.x = element_line(color = "black",linewidth = 0.8),
legend.position = "right")+#图例设置
scale_y_continuous(expand = c(0,0),breaks = c(0,5,10,15,20,25))#设置刻度从0开始
p2
##拼图
p1%>%insert_right(p2,width = 2)
最后用AI微调即可得到下方效果图:
PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
完整代码(附带注释)及原始数据获取:
夸克网盘(手机端下载夸克APP进行搜索提取):
链接:https://pan.quark.cn/s/d5a0d114eb8d
提取码:6JUj