一、Spearman analysis #RGS1(cg10718027) setwd("G:/A/H/CO/CO2") spss <- read_csv("spss2.csv") ##read data library(ggplot2) ## library R packages library(ggpubr) p <- ggplot(data = spss, aes(x = cg10718027, y = RGS1, color = cyl)) + geom_point() + geom_smooth(method = lm) + scale_color_manual(values = c('#FF7400', '#009999')) + labs (title = 'iris') + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + stat_cor(method = 'spearman', aes(x = cg10718027, y = RGS1, color = cyl),label.x = 0.6) #Set the title, coordinate axes p<-p+ggtitle("RGS1(cg10718027)") +xlab ("Methylation level") + ylab ("Gene expression(mRNA)") + theme (title = element_text (family="sans", size=12, color="black",face="plain",hjust=0.5)) p<-p+theme (axis.text.x=element_text(family="sans", size=12,color="black",face="plain"),axis.text.y=element_text(family="sans",size=12,color="black",face="plain")) p<-p+theme(plot.title = element_text(family="sans",size = 12,hjust=0.5)) p<-p+theme(legend.title = element_text(family="sans",size = 12,hjust=0.5)) p #store the image ggsave("RGS1(cg10718027).png", width=5.5, height=4) 二、GSEA analysis setwd("G:/A/H/GSEA") rm (list = ls()) ###1. Install Packages library(devtools) install_local("G:/A/H/GSEA/Xinpeng-Zhou-msigdf-master.zip") ###2. library R packages library(ggplot2) library(clusterProfiler) library(tidyverse) library(msigdf) library(org.Hs.eg.db) library(enrichplot) # ##3. data reduction all_gene <- read_csv("G:/A/H/GSEA/GSEA3.csv", na = c("", "#NAME?", "Inf")) %>% dplyr::select(gene_id, log2FoldChange) %>% filter(!is.na(log2FoldChange)) # FC_4 <- read_csv("./data/RA-vs-OA-diff-pval-0.05-FC-4_gene.csv", na = c("","#NAME?")) # FC_2 <- read_csv("./data/RA-vs-OA-diff-pval-0.05-FC-2_gene.csv", na = c("","#NAME?")) id <- bitr(all_gene$gene_id, "SYMBOL", "ENTREZID", "org.Hs.eg.db") %>% mutate(gene_id = SYMBOL) %>% dplyr::select(gene_id, ENTREZID) %>% inner_join(all_gene) %>% dplyr::select(ENTREZID, log2FoldChange) %>% arrange(desc(log2FoldChange)) genelist <- id$log2FoldChange names(genelist) <- id$ENTREZID # 4. GSEA ## Molecular Signatures Database (MSigDB) ## C5 GO gene set c5 <- msigdf.human %>% filter(collection == "c5") %>% dplyr::select(geneset, entrez) %>% as.data.frame() gsea_c5 <- GSEA (genelist, TERM2GENE = c5) head(gsea_c5) write.csv (gsea_c5, file ="gsea_c5.csv") ridgeplot(gsea_c5, showCategory = 3, label_format = 30) ggsave ("ridgeplot.png", width=10, height=5) gseaplot (gsea_c5, 12, title = gsea_c5@result[["ID"]][12]) gseaplot2(gsea_c5, 12, title = gsea_c5@result[["ID"]][12], base_size=11) ggsave ("gsea_c5_12.png", width=7, height=5) 三、GO and KEGG analysis rm(list = ls()) setwd("G:/GEO") if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install(version = "3.15") BiocManager::install("clusterProfiler") BiocManager::install("org.Hs.eg.db") BiocManager::install("KEGG.db") library(stringr)###library R packages library(clusterProfiler) library(enrichplot) library(org.Hs.eg.db) library(KEGG.db) library(forcats) library(ggplot2) read.csv("G:/GEO/DE_genes") geneList <- DE_genes$logFC DE_genes$symbol <- rownames(DE_genes) names(geneList) <- DE_genes$symbol geneList <- sort(geneList, decreasing = TRUE);head(geneList) entreid <- bitr(names(geneList), fromType="SYMBOL", toType="ENTREZID", OrgDb="org.Hs.eg.db") names(geneList) <- entreid$ENTREZID gene <- names(geneList)[abs(geneList)>2] head(gene) kk <- enrichKEGG(gene = names(geneList), organism = 'hsa', pvalueCutoff = 0.05, qvalueCutoff = 0.05, use_internal_data = T) head(kk) nrow(kk) kegg_data <- kk@result p = ggplot(kegg_data,aes(-log(pvalue),Description)) p=p + geom_point() p=p + geom_point(aes(size=Count)) pbubble = p+ geom_point(aes(size=Count,color=-log10(pvalue))) pr = pbubble+scale_color_gradient(low="#FDE725FF",high = "#2C738EFF") pr = pr+labs(color=expression(-log(pvalue)),size="Count", x="-log10(pvalue)",y="Description",title="KEGG pathway analysis")+ scale_y_discrete(labels = function(x) str_wrap(x, width = 45) ) pr<-pr + theme_bw() pr<-pr+theme(axis.title.x =element_text(family="sans",size=27,color="black",face="plain"), axis.title.y=element_text(family="sans",size=27,color="black",face="plain")) pr<-pr+theme(legend.title = element_text(family="sans",size = 27,color="black",face="plain",hjust=0.5)) pr<-pr+theme(legend.text = element_text(family="sans",size = 25,color="black",face="plain",hjust=0.5)) pr<-pr+theme(plot.title = element_text(family="sans",face="bold",size =27,hjust=0.5)) print(pr) ggsave("KEGG1.png",width=14.5,height=14.5) ego <- enrichGO(gene = names(geneList), OrgDb = org.Hs.eg.db, ont = "ALL", pvalueCutoff = 0.05, pAdjustMethod = "BH", minGSSize = 10, maxGSSize = 500, readable = FALSE, pool = FALSE ) go_data <- ego@result p = ggplot(go_data,aes(-log(pvalue),Description)) p=p + geom_point() p=p + geom_point(aes(size=Count)) pbubble = p+ geom_point(aes(size=Count,color=-log10(pvalue))) pr = pbubble+scale_color_gradient(low="#FDE725FF",high = "#2C738EFF") pr = pr+labs(color=expression(-log(pvalue)),size="Count", x="-log10(pvalue)",y="Description",title="BP Term")+ scale_y_discrete(labels = function(x) str_wrap(x, width = 30) ) pr<-pr + theme_bw() pr<-pr+theme(axis.title.x =element_text(family="sans",size=26,color="black",face="plain"), axis.title.y=element_text(family="sans",size=26,color="black",face="plain")) pr<-pr+theme(axis.text.x =element_text(family="sans",size=26,color="black",face="plain"), axis.text.y=element_text(family="sans",size=26,color="black",face="plain")) pr<-pr+theme(legend.title = element_text(family="sans",size = 26,color="black",face="plain",hjust=0.5)) pr<-pr+theme(legend.text = element_text(family="sans",size = 20,color="black",face="plain",hjust=0.5)) pr<-pr+theme(plot.title = element_text(family="sans",face="bold",size =26,hjust=0.5)) print(pr) ggsave("GO.png",width=10.5,height=6.5) 四、multiVolcano setwd("C:/Users/runru/Desktop/H") library(magrittr) library(ggplot2) library(ggrepel) library(janitor) library(cowplot) Sys.setenv(LANGUAGE = "en") #Display English error message options(stringsAsFactors = FALSE) #Prohibit chr to factor data <- read.csv("C:/Users/runru/Desktop/H/VOL/DMSinput.csv", check.names=FALSE) row.names(data) <- data[, 1] colnames(data)[1] <- "Geneid" head(data) p1 <- ggplot(data, aes(deltaBeta, -log10(FDR))) + geom_text_repel(aes(deltaBeta, -log10(FDR), label = ifelse(-log10(FDR) > 60, rownames(data), ""))) + geom_point(aes(color = feature)) + guides(colour = guide_legend(override.aes = list(size=5))) + # Modify legend size theme(legend.position = "none") + # remove legend labs(x = "Methylation difference (beta)", y = bquote(~-log[10]~(italic("FDR")))) + #参考了FigureYa59Volcano theme(axis.title.x = element_text(color="black", size = 14, face = "bold"), axis.title.y = element_text(color="black", size = 14, face = "bold")) p1 # Remove background and gridlines p1 <- p1 + theme_classic() + theme(#panel.background = element_rect(fill = NA), panel.border = element_rect(color = NA, fill = NA, size = 2), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank()) + scale_y_continuous(breaks = c(0, 2.5, 5, 7.5, 10), labels = c(0, 2.5, 5, 7.5, ""), limits = c(0, 10)) p1 ``` ## Drawing of the column chart `p2` on the right side of the scatter plot ```{r} ### get percentage information Percent <- data[, -1] %>% tabyl(feature) %>% adorn_totals("row") %>% adorn_pct_formatting() %>% .[-9,] Percent # Get the maximum value of n of the count column in Percent n_max = max(Percent$n) n_max ### Construct data for drawing colored dots # Note that the filling here should be the same as the previous order, that is to say, the color should correspond to the color in the scatter plot Percent$A <- 1:8 test = data.frame(x = c(1, 2, 3, 4, 5, 6, 7,8), y = rep(-n_max/15, 8), type = LETTERS[1:8]) rect_data <- data.frame( xstart = c(0.55, 1.55, 2.55, 3.55, 4.55, 5.55, 6.55), xend = c(1.45, 2.45, 3.45, 4.45, 5.45, 6.45, 7.45), ystart = c(rep(-n_max/2, 7)), yend = c(rep(0, 7))) ### draw p2 <- ggplot(Percent, aes(A, n)) + geom_bar(stat = "identity", fill = "#BEBEBE", color = "black") + geom_point(data = test, aes(x, y, color = type), size = 4) + geom_text(aes(label = Percent$percent, hjust = ifelse(n > 1000, 2, -0.2)), #nudge_y = -120, vjust = 0, fontface = "bold", size = 3) + geom_text(data = Percent, aes(A, -n_max/3, label = c("1stExon", "3'UTR", "5'UTR", "Body", "IGR", "TSS1500", "TSS200")), color = "black", fontface = "bold", size = 3, hjust = 0.5) + geom_rect(data = rect_data, inherit.aes = FALSE, aes(xmin = xstart, xmax = xend, ymin = ystart, ymax = yend), fill = "NA", color = "black") + theme_bw() + coord_flip() + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + theme(panel.background = element_rect(fill = NA), panel.border = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(), axis.line.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank(), legend.position = "none" ) + guides(fill = FALSE) + xlab("") + ylab("") p2 ``` ## Drawing of top percentage graph `p3` ### Calculate the percentage percent_Hypo <- sum(data$deltaBeta > 0) / nrow(data) paste("Hypo was", round(percent_Hypo * 100, 2), "%" ) percent_Hyper <- sum(data$deltaBeta < 0) / nrow(data) paste("Hyper was", round(percent_Hyper * 100, 2), "%" ) df1 <- data.frame(xmin = c(-350, 500, 1928, 4591), xmax = c(500, 1928, 4591, 6500), ymin = 00, ymax = 100, class = c("A","B","C","A"), text = c(paste(round(percent_Hyper * 100, 2), "%" ), "", "", paste(round(percent_Hypo * 100, 2), "%" ))) df1 ### draw p3 <- ggplot(df1, aes(xmin= xmin, xmax = xmax, ymin= ymin , ymax= ymax, fill = class, label = text )) + geom_rect(color = "black") + geom_text(aes(label = df1$text, x = xmin + 400, y = ymax - 50), position = position_dodge(0.9), vjust = 0, fontface = "bold", size = 3) + scale_fill_manual(values = c("#FFFFFF", "#4D4D4D", "#BEBEBE", "#FFFFFF")) + theme_bw() + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + theme(panel.background = element_rect(fill = NA), panel.border = element_rect(color = NA, fill = NA, size = 2), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(), axis.line.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank()) + guides(fill = FALSE) + xlab("") + ylab("") p3 `` ## Group picture ```{r,fig.width = 10,fig.height = 6} combined_plot <- insert_xaxis_grob(p1, p3, position = "top", height = grid::unit(0.4, "in") # Adjust the height ratio ) combined_plot <- insert_yaxis_grob(combined_plot, p2, position = "right", width = grid::unit(2.5, "in") ) # ggdraw(combined_plot) subtitle_theme_1 <- ggdraw() + draw_label("Hyermethylated DMPs Hypomethylated DMPs", x = 0.1, y = 0.25, hjust = 0, vjust = 1.01, size = 10, fontface = "bold") p <- plot_grid(subtitle_theme_1, combined_plot, ncol = 1, rel_heights = c(0.1, 1)) # Top percentage and scatter scale p # save to file ggsave("multiVolcano.pdf")