气泡图(bubble chart)与散点图类似,绘制时将一个变量放在横轴,另一个变量放在纵轴,而第三个变量则用气泡的大小来表示,可用于展示三个变量之间的关系,如生物狗常用的 — Pathway富集分析气泡图。
# 交互式文件夹选择,运行后依次打开文件夹,目的文件的路径被存在file里面
file <- file.choose()
# 读取文件 sep 根据文件格式确定
pathway_data <- read.table(file,header = TRUE, sep = "\t")
library(ggplot2)
# 做主图
bubble <- ggplot(data = pathway_data, aes(x = richFactor, y = Pathway)) +
geom_point(aes(size = R0vsR3,color = -1*log10(Qvalue))) +
scale_color_gradient(low = "green", high = "red")
# 字体修饰
windowsFonts(myFont = windowsFont("Times New Roman"))
# 修改细节 — 图注,点大小,点shape
bubble_style <- bubble + theme_classic()+
labs(
title =
"Pathway Enrichment",
caption =
"Data from http://www.biotrainee.com/thread-927-1-1.html",
x = "Rich Factor",
y = "Pathway Name",
color="-log10(Qvalue)", # 颜色图注名
size="Gene number")+ # 大小图注名
scale_size(range = c(0, 10)) + #等比修改圆圈大小
theme(plot.title=element_text(family="myFont",size=12,
color="red",face="italic",
hjust=0.5,lineheight=0.5),
plot.subtitle = element_text(hjust = 0.5))