缘由
生信之路,道阻且长,记录经验,但渡有缘人。
碎碎念
及时当勉励,岁月不饶人 ——陶渊明
炫图
送
代码
1. 数据准备部分
data <- read.delim("~/arrow.txt")- 功能:读取一个外部文件
arrow.txt,其中包含用于绘制箭头和线段的数据。 data数据结构假设:应包含以下列:shape: 决定绘制形状,如箭头(arrow)或线段(line)。regu: 调节方向(如"up"表示上调,"down"表示下调)。x,y,y_end: 确定线段或箭头的起点和终点坐标。
data2 <- data.frame(
x = rep(1:4),
y = rep(paste0("pathway", 1:9), 4),
pvalue=runif(36, min = 0, max = 1)
)- 功能:创建模拟数据
data2,用于散点图绘制。x:离散分组变量(1-4)。y:不同pathway分组。pvalue:随机生成的 P 值,用于定义散点的颜色和大小。
2. 上部箭头和线段绘图(p 图表)
p <- ggplot(data, aes(x=x, y=label)) +
geom_segment(
data = subset(data, shape == "arrow" & regu=="up"),
aes(x = x, y = y, xend = x, yend = y_end),
arrow = arrow(length = unit(0.2, "cm")),
color = "red", size = 2
) +
geom_segment(
data = subset(data, shape == "arrow" & regu=="down"),
aes(x = x, y = y, xend = x, yend = y_end),
arrow = arrow(length = unit(0.2, "cm")),
color = "blue", size = 2
) +
geom_segment(
data = subset(data, shape == "line"),
aes(x=x-0.3, xend = x + 0.3, y=y, yend = y_end),
color = "gray70", size = 2
)- 功能:绘制线段和箭头,区分形状及方向。
- 箭头:根据
shape和regu列,绘制红色(上调)和蓝色(下调)箭头。 - 水平线段:根据
shape=="line"绘制灰色线段。
- 箭头:根据
p <- p + scale_y_continuous(breaks =c(1.5, 3.5) , labels = c("protein","mRNA"), position = "right") +
labs(x =NULL, y =NULL) + theme_minimal() +
theme(panel.grid = element_blank(),
axis.text.x=element_blank(),text=element_text(size=18)) - 功能:优化坐标轴及图表样式。
- Y轴:
- 设置断点为 1.5 和 3.5,并分别标记为
"protein"和"mRNA"。 - 坐标轴显示在图表右侧(
position = "right")。
- 设置断点为 1.5 和 3.5,并分别标记为
- 全局样式:
- 去掉网格线(
panel.grid = element_blank())。 - 隐藏 X 轴刻度文本(
axis.text.x = element_blank())。
- 去掉网格线(
- Y轴:
3. 下部散点图绘制(p_main 图表)
p_main <- ggplot(data2, aes(x = x, y = y)) +
geom_point(aes(color = pvalue, size = pvalue)) +
labs(x = NULL, y = NULL) +
theme_minimal() +
scale_color_gradient(low = "#ee0000", high = "#0077d7")- 功能:绘制散点图。
- 颜色渐变:基于
pvalue,从红色(#ee0000)到蓝色(#0077d7)。 - 点大小:根据
pvalue调整点的大小。
- 颜色渐变:基于
p_main <- p_main + scale_y_discrete(position = "right") +
scale_x_continuous(position = "top") +
theme(
panel.grid = element_blank(),
panel.border = element_blank(),
text = element_text(size = 18),
axis.line.x = element_line(color = "black", size = 0.5, linetype = "solid") ,
axis.line.y = element_blank(),
axis.text.x= element_blank()
) +
guides(
color = guide_legend("pvalue"),
shape = guide_legend("pvalue")
)- 功能:调整散点图样式。
- 坐标轴位置:X 轴移到顶部,Y 轴移到右侧。
- 图表装饰:去掉边框和网格线,仅保留顶部的 X 轴线。
- 图例:为
pvalue添加图例。
4. 图表组合
p %>% insert_bottom(p_main, height = 5)- 功能:使用
aplot包将p(箭头和线段图)与p_main(散点图)组合在一起,将散点图插入到箭头图的底部。 height = 5:指定底部散点图所占高度。
ALL
library(aplot)
data <- read.delim("~/arrow.txt")
set.seed(123) # 设置随机种子保证结果可复现
data2 <- data.frame(
x = rep(1:4), # x 轴值
y = rep(paste0("pathway", 1:9), 4),
pvalue=runif(36, min = 0, max = 1)
)
p <- ggplot(data, aes(x=x, y=label)) +
geom_segment(
data = subset(data, shape == "arrow" & regu=="up"), # 只绘制箭头的数据
aes(x = x, y = y, xend = x, yend = y_end),
arrow = arrow(length = unit(0.2, "cm")), # 添加箭头
color = "red", size = 2
) +
geom_segment(
data = subset(data, shape == "arrow" & regu=="down"), # 只绘制箭头的数据
aes(x = x, y = y, xend = x, yend = y_end),
arrow = arrow(length = unit(0.2, "cm")), # 添加箭头
color = "blue", size = 2
) +
geom_segment(
data = subset(data, shape == "line"),
aes(x=x-0.3, xend = x + 0.3, y=y, yend = y_end), # 只绘制水平线段的数据
color = "gray70", size = 2
)
p <- p + scale_y_continuous(breaks =c(1.5, 3.5) , labels = c("protein","mRNA"), position = "right") +
labs(x =NULL, y =NULL) + theme_minimal()+
theme(panel.grid = element_blank(),
axis.text.x=element_blank(),text=element_text(size=18))
# 绘制散点图并使用 facet_wrap 分组
p_main <- ggplot(data2, aes(x = x, y = y)) +
geom_point(aes(color = pvalue, size = pvalue)) + # 绘制散点图
labs(x = NULL, y = NULL) +
theme_minimal() +
scale_color_gradient(low = "#ee0000", high = "#0077d7")
p_main <- p_main + scale_y_discrete(position = "right") +
scale_x_continuous(position = "top") +
theme(
panel.grid = element_blank(), # 去除网格线
panel.border = element_blank(),
text = element_text(size = 18), # 设置文本大小
axis.line.x = element_line(color = "black", size = 0.5, linetype = "solid") ,
axis.line.y = element_blank(), # 设置顶部 X 轴线条
axis.text.x= element_blank() ,
) +
guides(
color = guide_legend("pvalue"),
shape = guide_legend("pvalue")
)
p %>% insert_bottom(p_main,height = 5)
p/p_main