【傻瓜美圖】ggplot2熱圖

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)))
#預(yù)覽數(shù)據(jù);
head(hd_ac)

#轉(zhuǎn)成數(shù)據(jù)框;
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)
#轉(zhuǎn)成長(zhǎng)形數(shù)據(jù);
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")
#預(yù)覽作圖數(shù)據(jù);
head(dt_ac)

轉(zhuǎn)成因子,避免ggplot2自動(dòng)排序;
#為了確保繪圖順序從上到下,可以顛倒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調(diào)整格子線粗細(xì),color調(diào)整格子線顏色;
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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容