title: "ggplot2绘制火山图"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
第一步:初步了解ggplot2
创建画布
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj)))
在画布上添加图层(几何图像,散点图
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point()
将direction映射给点的颜色
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction))
换个主题//
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction)) +
theme_bw()
(标度控制映射的方式[颜色]
library(ggplot2)
library(ggsci)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction)) +
scale_color_npg()+
theme_bw()
ggplot2涉及到的思想:
1.图层2.映射3.标度4.主题
步骤二:深入理解
修改点的颜色
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction))+
scale_color_manual(values = my_palette)+
theme_bw()
修改点的大小
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange)))+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
theme_bw()
修改点的透明度
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange),alpha = abs(log2FoldChange)))+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
theme_bw()
#修改点的透明度
```{r}
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange),alpha = abs(log2FoldChange)))+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
theme_bw()
添加阈值线条
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange),alpha = abs(log2FoldChange)))+
geom_hline(yintercept = -log10(0.05),linetype = 'dashed')+
geom_vline(xintercept = c(-1,1),linetype = 'dashed')+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
theme_bw()
标题居中
library(ggrepel)
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange),alpha = abs(log2FoldChange)))+
geom_hline(yintercept = -log10(0.05),linetype = 'dashed')+
geom_vline(xintercept = c(-1,1),linetype = 'dashed')+
geom_label_repel(data = top_de,aes(label = Gene_Symbol))+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
labs(x = 'log2 fold change',
y = '-log10 (pvalue)',
title = 'volcano plot',
size = 'log2 fold change')+
theme_bw()+
theme(plot.title = element_text(size = 20 ,hjust = 0.5))
删掉一个图例&修改图例位置
library(ggrepel)
my_palette <- c('#66CC33','#9999CC','#FF6699')
library(ggplot2)
ggplot(data = de_result , aes(x = log2FoldChange , y = -log10(padj))) +
geom_point(aes(color = direction, size = abs(log2FoldChange),alpha = abs(log2FoldChange)))+
geom_hline(yintercept = -log10(0.05),linetype = 'dashed')+
geom_vline(xintercept = c(-1,1),linetype = 'dashed')+
geom_label_repel(data = top_de,aes(label = Gene_Symbol))+
scale_size(range = c(0.1,3))+
scale_color_manual(values = my_palette)+
labs(x = 'log2 fold change',
y = '-log10 (pvalue)',
title = 'volcano plot',
size = 'log2 fold change',
color = ' ')+
#color = ' ' 将图例中direction去掉
theme_bw()+
theme(plot.title = element_text(size = 20 ,hjust = 0.5),
legend.background = element_blank(),
legend.position = c(0.9,0.6))+
guides(size = FALSE)