【傻瓜美图】ggplot2热图
2023-08-24 本文已影响0人
pudding815
ac <- read.csv("D:/High-throughput sequencing/!!!!GSE167940/RNA-seq/组蛋白修饰酶/ac.csv",header = T,row.names = 1)
me3 <- read.csv("D:/High-throughput sequencing/!!!!GSE167940/RNA-seq/组蛋白修饰酶/组蛋白修饰酶.csv",header = T,row.names = 1)
library(ggplot2)
library(tidyr)
library(dplyr)
#按行归一化;
hd_ac = t(scale(t(ac)))
hd_me3 = t(scale(t(me3)))
#预览数据;
head(hd_ac)
#转成数据框;
gene_ac <- row.names(hd_ac)
hf_ac <- data.frame(gene_ac,hd_ac)
gene_me3 <- row.names(hd_me3)
hf_me3 <- data.frame(gene_me3,hd_me3)
#转成长形数据;
dt_ac <- hf_ac %>% pivot_longer(
cols = !gene_ac,
values_drop_na = FALSE,
names_to = "groups",
values_to = "expressions")
dt_me3 <- hf_me3 %>% pivot_longer(
cols = !gene_me3,
values_drop_na = FALSE,
names_to = "groups",
values_to = "expressions")
#预览作图数据;
head(dt_ac)
转成因子,避免ggplot2自动排序;
#为了确保绘图顺序从上到下,可以颠倒level的顺序;
dt_ac$gene_ac <- factor(dt_ac$gene_ac,
levels = rev(unique(dt_ac$gene_ac)),
ordered = T)
dt_me3$groups <- factor(dt_me3$groups,
levels = unique(dt_me3$groups),
ordered = T)
#绘制热图,linewidth调整格子线粗细,color调整格子线颜色;
p_ac <- ggplot(dt_ac, aes(groups, gene_ac)) +
geom_tile(aes(fill = expressions),linewidth=0.4,
height=1,width=1,
colour = "black")+
scale_fill_gradient2(low = "#3FA9F5",
mid = "white",
high = "#FF931E",
midpoint = 0.5,
na.value = "grey92",
name="Exp")+
scale_x_discrete(position = "bottom")+
scale_y_discrete(position = "right")+
xlab('')+ylab('')+
theme(axis.text.x.bottom= element_text(angle = 45,face = "bold",
size=8,
hjust=0.3,vjust =0.5),
axis.text.y.right = element_text(angle = 0,face = "bold",
size=8,
hjust=1,vjust = 0.5),
panel.background = element_blank(),
legend.title = element_text(size = 6),
legend.position="left")+
guides(fill = guide_colourbar(direction = "vertical",
title.hjust=0,
title.position ="top",
ticks.colour="white",
frame.colour="purple",
barheight=5,
barwidth=0.7))+
coord_fixed(ratio = 1,expand=T)+
ggtitle("H3K27ac")
p_ac
p_me3 <- ggplot(dt_me3, aes(groups, gene_me3)) +
geom_tile(aes(fill = expressions),linewidth=0.4,
height=1,width=1,
colour = "black")+
scale_fill_gradient2(low = "#3FA9F5",
mid = "white",
high = "#FF931E",
midpoint = 0.5,
na.value = "grey92",
name="Exp")+
scale_x_discrete(position = "bottom")+
scale_y_discrete(position = "right")+
xlab('')+ylab('')+
theme(axis.text.x.bottom= element_text(angle = 45,face = "bold",
size=8,
hjust=0.3,vjust =0.5),
axis.text.y.right = element_text(angle = 0,face = "bold",
size=8,
hjust=1,vjust = 0.5),
panel.background = element_blank(),
legend.title = element_text(size = 6),
legend.position="left")+
guides(fill = guide_colourbar(direction = "vertical",
title.hjust=0,
title.position ="top",
ticks.colour="white",
frame.colour="purple",
barheight=5,
barwidth=0.7))+
coord_fixed(ratio = 1,expand=T)+
ggtitle("H3K4me3")
p_me3