大家好,我是带我去滑雪!
一幅精美的科研绘图会有诸多益处,精美的图像可以更好地传达研究结果和数据分析的重要信息。通过使用清晰、直观和易于理解的图像,可以更好地向读者展示研究的发现,有助于读者理解和解释数据。还可以增加研究报告或论文的可视化效果,使其更具吸引力和可读性。在学术界中,精美的图像常常吸引编辑和评审人员的注意,并提高研究发表的机会。此外,在学术会议或研究展示中展示精美图像也有助于引起其他研究者的兴趣和讨论。
本文分别介绍使用R语言中的ggplot包绘制气泡图、使用ComplexHeatmap包绘制带有显著性标记的热力图以及使用ggplot2包绘制渐变曲线图。
目录
1、使用ggplot绘制气泡图
(1)设置工作路径、安装并调用相关包
(2)导入数据
(3)绘制气泡图
2、使用ComplexHeatmap包绘制带有显著性标记的热力图
(1)安装并调用相关包
(2)导入数据集
(3)计算相关系数,并将结果中的P值替换为显著性标记
3、绘制渐变曲线图
(1)安装并调用包、导入数据
(2)绘制曲线图
(3)为图片添加渐变效果
1、使用ggplot绘制气泡图
(1)设置工作路径、安装并调用相关包
rm(list=ls())#清除全局环境变量
setwd('E:/工作/硕士/博客/博客39-使用R绘制气泡图、带有显著性标记的热力图、渐变曲线图')
install.packages("ggprism")
library(ggplot2)
library(ggprism)
library(RColorBrewer)
library(grid)
library(scales)
(2)导入数据
data=read.table("E:/工作/硕士/博客/博客39-使用R绘制气泡图、带有显著性标记的热力图、渐变曲线图/data.csv",header=TRUE,sep=",")
head(data)输出结果:
x1 x4 x9 x6
1 2 0 14 2
2 4 0 18 0
3 2 2 18 1
4 4 3 6 1
5 3 2 7 2
6 3 2 5 1
(3)绘制气泡图
ggplot(dd,aes(x1,x2,color=x3,size=x4,fill=x3))+
geom_point(color="blue",
#气泡边框色
shape=21,alpha=0.9)+
#形状
scale_size_continuous(range = c(1, 15))+
#气泡的相对大小
theme_bw()+
theme(panel.grid = element_blank(),
#添加画布背景
axis.line=element_line(),
#坐标轴的线设为显示
axis.text=element_text(color='black',size=12),
legend.text = element_text(color='black',size=12),
axis.title= element_text(size=12),
axis.text.x=element_text(vjust = 1,hjust = 1),
legend.key = element_blank())+
scale_fill_manual(values=c("skyblue1","pink","turquoise1","red","sienna1"))+
#指定气泡颜色
labs(x = 'x', # 定义x轴文本
y = 'y')# 定义y轴文本#调整背景色
color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)
#添加背景
grid.raster(alpha(color, 0.2),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)输出结果:
2、使用ComplexHeatmap包绘制带有显著性标记的热力图
(1)安装并调用相关包
install.packages("ComplexHeatmap")
install.packages("psych")
install.packages("ComplexHeatmap")
install.packages("BiocManager")
install.packages("circlize")
library(psych)
library(BiocManager)
if (!require("BiocManager"))
install.packages('BiocManager')
if (!require("ComplexHeatmap"))
BiocManager::install('ComplexHeatmap')
library(ComplexHeatmap)
library(circlize)
(2)导入数据集
dd=read.table("E:/工作/硕士/博客/博客39-使用R绘制气泡图、带有显著性标记的热力图、渐变曲线图/data1.csv",header=TRUE,sep=",")
head(dd)输出结果:
x1 x2 x3 x4 x5 x6 x7 x8 x9 y1
1 2 2 78.60 0 1 2 1 58 14 210
2 4 2 98.00 0 1 0 1 2337 18 433
3 2 1 58.10 2 1 1 1 25 18 255
4 4 2 118.00 3 0 1 0 2106 6 195
5 3 1 97.70 2 0 2 0 1533 7 150
6 3 2 115.94 2 1 1 1 47 5 570
(3)计算相关系数,并将结果中的P值替换为显著性标记
ct1 <- corr.test(dd,method = "pearson")
#提取相关系数
r2 <- ct1$r
#提取p值矩阵;
p2 <- ct1$p
#使用显著性星号标记进行替换;
p2[p2>=0 & p2 < 0.001] <- "***"
p2[p2>=0.001 & p2 < 0.01] <- "**"
p2[p2>=0.01 & p2 < 0.05] <- "*"
p2[p2>=0.05 & p2 <= 1] <- ""
p2输出结果:
x1 x2 x3 x4 x5 x6 x7 x8 x9 y1
x1 "***" "***" "***" "" "***" "" "" "" "***" "***"
x2 "***" "***" "***" "" "***" "" "*" "" "***" "***"
x3 "***" "***" "***" "" "***" "" "" "*" "***" "***"
x4 "" "**" "" "***" "*" "" "*" "*" "*" "***"
x5 "***" "***" "***" "**" "***" "***" "***" "" "***" "***"
x6 "" "" "" "" "***" "***" "**" "" "" ""
x7 "*" "***" "" "**" "***" "***" "***" "" "" "***"
x8 "" "*" "***" "**" "**" "" "" "***" "***" "***"
x9 "***" "***" "***" "**" "***" "*" "" "***" "***" "***"
y1 "***" "***" "***" "***" "***" "" "***" "***" "***" "***"
(4)绘图设置
range(r2)
#颜色映射函数
col_fun1 = colorRamp2(c(-0.1794705, 0, 1), c("#0f86a9", "white", "#FC8452"))
col_fun2 = colorRamp2(c(-0.1794705, 0, 1), c("#A5CC26", "white", "#FF7BAC"))
col_fun3 = colorRamp2(c(-0.1794705, 0, 1), c("#3FA9F5", "white", "#FF931E"))
col_fun4 = colorRamp2(c(-1, 0, 1), c("#ffa500", "white", "#B3A9EB"))
col_fun(seq(-2, 2))#热图格子大小设置;
cellwidth = 0.7
cellheight = 0.7
cn = dim(r2)[2]
rn = dim(r2)[1]
w=cellwidth*cn
h=cellheight*rn
#绘制热图显示显著性星号标记;
Heatmap(r2,name ="r", col = col_fun2,
#格子大小设置;
width = unit(w, "cm"),
height = unit(h, "cm"),
rect_gp = gpar(col = "white", lwd = 1.5),
border_gp = gpar(col = "#0f86a9",lty = 2,lwd = 1.2),
#聚类树样式设置;
column_dend_height = unit(1.5, "cm"),
row_dend_width = unit(1.5, "cm"),
column_dend_gp = gpar(col = "#0f86a9",lwd = 1.4),
row_dend_gp = gpar(col = "#0f86a9",lwd = 1.4),
#设置聚类gap数量和大小;
row_split = 2, column_split = 2,
row_gap = unit(2, "mm"),
column_gap = unit(2, "mm"),
#行列标签文字样式设置;
row_title = NULL,column_title = NULL,
column_names_gp = gpar(fontsize = 8),
row_names_gp = gpar(fontsize = 8),
#图例样式设置;
heatmap_legend_param = list(legend_height = unit(3, "cm"),
grid_width = unit(0.4, "cm"),
labels_gp = gpar(col = "gray20",
fontsize = 8)),
#显示星号标记设置;
#vjust垂直微调星号的位置;
cell_fun = function(j, i, x, y, width, height, fill) {
grid.text(p2[i, j], x, y, vjust = 0.7,
gp = gpar(fontsize = 13,col="white"))
})
输出结果:
换一种样式:
#绘制热图,显示相关性系数,保留两位小数;
Heatmap(r2,name ="r", col = col_fun1,
#格子大小设置;
width = unit(w, "cm"),
height = unit(h, "cm"),
rect_gp = gpar(col = "white", lwd = 1.5),
border_gp = gpar(col = "#0f86a9",lty = 2,lwd = 1.2),
#聚类树样式设置;
column_dend_height = unit(1.5, "cm"),
row_dend_width = unit(1.5, "cm"),
column_dend_gp = gpar(col = "#0f86a9",lwd = 1.4),
row_dend_gp = gpar(col = "#0f86a9",lwd = 1.4),
#设置聚类gap数量和大小;
row_split = 2, column_split = 2,
row_gap = unit(2, "mm"),
column_gap = unit(2, "mm"),
#行列标签文字样式设置;
row_title = NULL,column_title = NULL,
column_names_gp = gpar(fontsize = 8),
row_names_gp = gpar(fontsize = 8),
#图例样式设置;
heatmap_legend_param = list(legend_height = unit(3, "cm"),
grid_width = unit(0.4, "cm"),
labels_gp = gpar(col = "gray20",
fontsize = 8)),
#显示数值设置;
#i,j对应数据矩阵的行和列索引;
#x,y对应热图cell中心点坐标;
cell_fun = function(j, i, x, y, width, height, fill) {
grid.text(sprintf("%.2f", r2[i, j]), x, y,
gp = gpar(fontsize = 6))
})输出结果:
3、绘制渐变曲线图
(1)安装并调用包、导入数据
install.packages("ggplot2")
library(ggplot2)
setwd('E:/工作/硕士/博客/博客39-使用R绘制气泡图、带有显著性标记的热力图、渐变曲线图')
data=read.table("E:/工作/硕士/博客/博客39-使用R绘制气泡图、带有显著性标记的热力图、渐变曲线图/data2.csv",header=TRUE,sep=",")
(2)绘制曲线图
pdf("plot.pdf",width = 5.5, height = 3.7)
ggplot(data,aes(x=X,y=Y,fill=type))+
geom_line(color = "#dcedf5")+ #线图
coord_cartesian(xlim = c(4.5, 110))+ #设置X轴显示范围
scale_y_continuous(breaks = seq(0,10,2))+ #设置y轴刻度范围和间隔
scale_x_continuous(breaks = seq(0,100,20))+ #设置x轴刻度范围和间隔
labs(x="Hz",
y="Sb")+ #设置坐标轴标题
theme_bw()+ #设置主题
theme(panel.grid = element_blank(), #取消背景网格
axis.text = element_text(size = 10), #设置刻度值字体大小
axis.title = element_text(face = "bold") #设置字体
)+
geom_segment(aes(x = 55, y = 1, xend = 78, yend = 5),size= 0.3) +
geom_segment(aes(x = 48, y = 5, xend = 78, yend = 7.5),size= 0.3) +
geom_segment(aes(x = 46, y = 8, xend = 76, yend = 8.5),size= 0.3) +
geom_segment(aes(x = 47, y = 9.5, xend = 74, yend = 9.5),size= 0.3) +
geom_text(aes(x = 87, y = 5, label = "noise"))+
geom_text(aes(x = 84, y = 7.5, label = "3.3 pW"))+
geom_text(aes(x = 82, y = 8.5, label = "20 pW"))+
geom_text(aes(x = 80, y = 9.5, label = "78 pW"))
dev.off()输出结果:
(3)为图片添加渐变效果
这里使用 Adobe Illustrator软件进行处理,博主最近也在学习这个软件,可以一起交流!Adobe Illustrator在科研中作为一款专业的矢量绘图软件,可以提供强大的绘图、插图、编辑和设计功能,帮助科研人员创建高质量、专业外观的科学图表、插图、海报和展示资料,以及进行图像修饰和文字设计。它能够满足科研人员对图像设计和编辑的各种需求,并提升研究成果的可视化和呈现效果。
处理后的渐变曲线图:
需要数据集的家人们可以去百度网盘(永久有效)获取:
链接:https://pan.baidu.com/s/1E59qYZuGhwlrx6gn4JJZTg?pwd=2138
提取码:2138
--来自百度网盘超级会员V5的分享
更多优质内容持续发布中,请移步主页查看。
点赞+关注,下次不迷路!