绘图代码
1.创建数据
set.seed(123)
genes <- c("TP53", "BRCA1", "EGFR", "VEGFA", "APOE", "IL6", "TNF", "INS", "ESR1", "MTHFR")
samples <- c(paste("CK",1:5, sep = ""),
paste("Treat",1:5, sep = ""))
data_matrix <- matrix(runif(100, min = -2,max = 2), nrow = 10,
dimnames = list(genes, samples))
- 转换数据
data_df <- as.data.frame(as.table(data_matrix))
- 绘制基础图形
ggplot(data_df, aes(Var2, Var1, fill=Freq)) +
geom_tile() +
geom_text(aes(label =round(Freq,2)), color = "black",size = 3# 修改图中字体大小
)+
scale_fill_gradient2(low="#9ACAE8",mid = "white", high="#F5C1C8", midpoint = 0,
## 取值范围
limits = c(-2,2),
## 修改
name = "TPM",
# 自定义颜色条的刻度
breaks = c(-2,0,2),
) +
labs(x = NULL, y = NULL, title = NULL)+
theme_minimal()+
theme(
# plot.title = element_text(size = 16), # 修改标题的字体大小
# axis.title.x = element_text(size = 14), # 修改x轴标签的字体大小
# axis.title.y = element_text(size = 14), # 修改y轴标签的字体大小
axis.text.y = element_text(size = 10), # 修改y轴刻度标签的字体大小
legend.title = element_text(size = 10), # 修改图例标题的字体大小
legend.text = element_text(size = 10), # 修改图例文本的字体大小
axis.text.x = element_text(size = 12,angle = 0, hjust = 1)) # 旋转x轴标签,使其可读性更强
附件:image.png
- 只显示指定基因表达量
ggplot(data_df, aes(Var2, Var1)) +
geom_tile(aes(fill = ifelse(Var1 == "BRCA1", Freq, Freq))) + # 使用Freq作为默认值
geom_text(aes(label = ifelse(Var1 == "BRCA1", round(Freq, 2), "")), color = "black", size = 3) + # 仅显示BRCA1基因的表达量
scale_fill_gradient2(low="#9ACAE8",mid = "white", high="#F5C1C8", midpoint = 0,
## 取值范围
limits = c(-2,2),
## 修改
name = "TPM",
# 自定义颜色条的刻度
breaks = c(-2,0,2),
) +
labs(x = NULL, y = NULL, title = NULL)+
#theme_minimal()+
theme_bw()+
theme(
# plot.title = element_text(size = 16), # 修改标题的字体大小
# axis.title.x = element_text(size = 14), # 修改x轴标签的字体大小
# axis.title.y = element_text(size = 14), # 修改y轴标签的字体大小
axis.text.y = element_text(size = 10), # 修改y轴刻度标签的字体大小
legend.title = element_text(size = 10), # 修改图例标题的字体大小
legend.text = element_text(size = 10), # 修改图例文本的字体大小
axis.text.x = element_text(size = 12,angle = 0, hjust = 1)) # 旋转x轴标签,使其可读性更强
- 显示多个基因
6. 旋转图形
p <- ggplot(data_df, aes(Var2, Var1, fill=Freq)) +
geom_tile() +
scale_fill_gradient2(low="#9ACAE8",mid = "white", high="#F5C1C8", midpoint = 0,
## 取值范围
limits = c(-2,2),
## 修改
name = "TPM",
# 自定义颜色条的刻度
breaks = c(-2,0,2),
) +
labs(x = NULL, y = NULL, title = NULL)+
theme_minimal()+
theme(
axis.text.y = element_text(size = 10, color = "black"),
legend.title = element_text(size = 10, color = "black"),
legend.text = element_text(size = 10, color = "black"),
axis.text.x = element_text(size = 12, color = "black",
angle = 0, hjust = 1))
# 将ggplot对象转换为grob对象
g <- ggplotGrob(p)
# 创建一个弯曲的视图端口
pushViewport(viewport(width=unit(0.8, "npc"), height=unit(0.8, "npc"), angle=60))
# 绘制弯曲的热图
grid.draw(g)
获得本教程代码和数据,后台回复关键词:20240612
若我们的教程对你有所帮助,请点赞+收藏+转发**,这是对我们最大的支持。
往期部分文章
「1. 最全WGCNA教程(替换数据即可出全部结果与图形)」
「2. 精美图形绘制教程」
「3. 转录组分析教程」
「4. 转录组下游分析」
「小杜的生信筆記」 ,主要发表或收录生物信息学教程,以及基于R分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!