其他
跟着Nature学绘图——热图+显著性+间隔+注释+柱状图!
点击上方
“科研后花园”
关注我们
复现图片如下:
主要复现的图片是上图中的图c中样式的图片,具体代码如下:
1、设置工作环境并加载所需R包:
rm(list=ls())#clear Global Environment
setwd("D:/桌面/test")
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package
2、加载绘图数据(根据原文图片随机编写数据,无实际意义):
data1 <- read.table("data1.txt",sep="\t",header = T,check.names = F)
data2 <- read.table("data2.txt",sep="\t",header = T,check.names = F)
3、数据清洗及格式转换:
data1$Y <- c(1,2,3,4)
data2$Y <- c(1,2,3,4)
df1 <- melt(data1,id.vars = c("group","Y"))
df1$X <- rep(1:9, each = 4)
df1$gap <- rep(c(1,2,3),times = c(4,4,28))
4、绘图:
##绘制基本热图
p1 <- ggplot(df1, aes(X, Y)) +
geom_rect(aes(xmin = X-0.5, xmax = X+0.5, ymin = Y-0.5, ymax = Y+0.5), color = "grey40",fill="white") +
geom_point(aes(size= ifelse(value > 0, value, 0),color=ifelse(value > 0, value, 0)))+
scale_color_continuous(low = "white", high = "#23589e") +
geom_point(data = df1[df1$value == 0, ], shape = 21, size = 1, color = "black",fill="grey50")+
geom_text(data = subset(df1, value > 0 & value < 0.5),
aes(label = "NS"), size=4,color="#92461f",vjust=-0.1)+
scale_x_continuous(position = "top",breaks = c(1:9), labels = c("Overall transm.", "CosteaPl_2017_DEU", "BritolL_2016",
"Guinea-Bissau", "PasolliE_2018_MDG","PehrssonE_2016_PER",
"PehrssonE_2016_SLV","Ghana","Tanzania")) +
scale_y_continuous(breaks = c(1:4), labels = data1$group)+
theme_void()+
theme(axis.text.x = element_text(angle = 45,hjust = 0,vjust = 0,size=10,color="black"),
axis.text.y = element_text(color="black",size=10,vjust = 0,hjust = 1))+
labs(x=NULL,y=NULL,color="SGB transmissibility")+
guides(size = "none")+
scale_size_continuous(range = c(1,8))+
facet_grid(~gap,scales = 'free',space = "free")+
theme(strip.text = element_blank())+
geom_rect(data = df1[df1$gap == 2, ], aes(xmin = 1.5, xmax = 2.5, ymin = -Inf, ymax = 0.3),
fill = "#6c3417")+
geom_rect(data = df1[df1$gap == 3, ], aes(xmin = 2.5, xmax = 9.5, ymin = -Inf, ymax = 0.3),
fill = "#68a030")
#绘制柱状图
p2 <- ggplot(data2,aes(Y,value))+
geom_col(fill="#b2b2b2",width = 0.8)+
theme_classic()+
theme(axis.text.x = element_text(color = "black",size=12),
axis.ticks.x = element_line(color = "black",linewidth=0.8),
axis.line.x = element_line(color = "black",linewidth=0.8),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.y = element_blank(),
plot.title = element_text(color="black",hjust = 0.5,size=15),
plot.background = element_blank())+
coord_flip()+
labs(x=NULL,y=NULL,title = "Prevalence(%)")+
scale_y_continuous(expand = c(0,0))
###拼接图形
p1%>%aplot::insert_right(p2,width = 0.2)
得到和示意图大致相同的图形:
5、最后使用AI或者PS调整图形细节即可:
PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
完整代码(附带注释)及原始数据获取:
夸克网盘(手机端下载夸克APP进行搜索提取):
链接:https://pan.quark.cn/s/a3af5c3f9ae3
提取码:W1wY