前言

这一部分会简单分享一些R语言画图的小技巧,后续相应的代码以及测试数据文件都已经上传到百度网盘,可以在**公众号(生信技术)**留言回复“ R语言 ”获取。

R包安装

需要安装 ggplot2, qqman, gplots, pheatmap, scales, reshape2, RColorBrewer 和 plotrix(使用 install.packages(), 如 install.packages('ggplot2')

下面是后续需要用到的一些R包,如果需要新下载后面文章会继续介绍。

1
2
3
4
5
6
7
8
9
10
library(ggplot2)
library(qqman)
library(gplots)
library(pheatmap)
library(scales)
library(reshape2)
library(RColorBrewer)
library(plyr)
library(plotrix)
library(ggpubr) #用于带统计检验的箱线图

加载数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Read the input files
# “header=T” means that the data has a title, and sep="\t" is used as the separator
data <-read.table("box_plots_mtcars.txt",header=T,sep="\t")
# The function c(,,) means create the vector type data
df <- data[, c("mpg", "cyl", "wt")]

df2 <-read.table("histogram_plots.txt",header=T,sep="\t")

df3 <- read.table("volcano_plots.txt", header=T)

df4 <- read.table("manhattan_plots_gwasResults.txt",header=T,sep="\t")

df5 <-read.table("heatmaps.txt",header=T,sep="\t")

# Covert data into matrix format
# nrow(df5) and ncol(df5) return the number of rows and columns of matrix df5 respectively.
dm <- data.matrix(df5[1:nrow(df5),2:ncol(df5)])

# Get the row names
row.names(dm) <- df5[,1]

df6 <- read.table("ballon_plots_GO.txt", header=T, sep="\t")

保存并查看图片

如果要保存绘图,使用 pdf() + dev.off()ggsave()
第二个是特定于 ggplot2 包的(即,如果 plot 的代码以 ggplot 开头,那么可以使用第二个)。

让我们看一个例子:

  • pdf() + dev.off()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Begin to plot
# Output as pdf
pdf("1.1.Basic_boxplot.pdf", height = 3, width = 3)
# Mapping the X and Y
# Components are constructed by using "+"
ggplot(df, aes(x=cyl, y=mpg))+
# draw the boxplot and fill it with gray
geom_boxplot(fill="gray")+
# Use the labs function to set the title and modify x and y
labs(title="Plot of mpg per cyl",x="Cyl", y = "Mpg")+
# Set the theme style
theme_classic()

# Save the plot
dev.off()
  • ggsave()
1
2
3
4
5
6
7
# Begin to plot
p <- ggplot(df, aes(x=cyl, y=mpg)) +
geom_boxplot(fill="gray")+
labs(title="Plot of mpg per cyl",x="Cyl", y = "Mpg")+
theme_classic()
# Sava as pdf
ggsave("1.1.Basic_boxplot.pdf", plot=p, height = 3, width = 3)

R包导出高质量可编辑图形

下面总结了一些输出高清可编辑图片的方式,更好的适用于科研投稿的高质量图片要求

  1. SCI常见格式

  2. 矢量图和位图

  3. ggsave函数(ggplot2包)

  4. Cairo包

  5. export包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#高质量图形的导出
#内置图形导出函数
png('P1.png',height = 1200,width = 1200,res=300) #投稿一般要求res分辨率300以上
ggplot(dsamp,aes(carat,price,fill= cut))+
geom_point(size __3.shane =21. color= 'gray30')+
theme_test()+
scale_fill_npg()+
theme(legend.position ='none')
dev.off()

###ggsave函数
ggplot(dsamp,aes(carat,price,fill= cut))+
geom_point(size = 3,shape =21, color ='gray30')+
theme_test()+
scale_fill_npg()+
theme(legend.position = 'none')
ggsave('p2.png',height = 10,width = 10,units = 'cm',dpi = 300)

###cairo包
install.packages('Cairo')
library(Cairo)
CairoPNG('P1.png',height = 1200,width = 1200,res=300)
ggplot(dsamp,aes(carat,price,fill= cut))+
geom_point(size = 3,shape =21, color ='gray30')+
theme_test()+
scale_fill_npg()+
theme(legend.position = 'none')
dev.off()


CairoPDF('p1.pdf',height=6,width =6)
ggplot(dsamp,aes(carat,price,fill= cut))+
geom_point(size = 3,shape =21, color='gray30')+
theme_test()+
scale_fill_npg()+
theme(legend.position = 'none')
dev.off()


###利用rstudio导出

##export包
install. packages('export')
library(export)
p <- ggplot(dsamp,aes(carat,price,fill= cut))+
geom_point(size =3,shape =21, color='gray30')+
theme_test()+
scale_fill_npg()+
theme(legend. position = 'none')
graph2ppt(p)

箱线图

基本箱线图制作

1
2
3
4
5
6
7
8
9
10
> df$cyl <- as.factor(df$cyl)

> head(df)
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
Valiant 18.1 6 3.460
1
2
3
4
5
6
ggplot(df, aes(x=cyl, y=mpg)) + 
geom_boxplot(fill="gray")+
labs(title="Plot of mpg per cyl(Test data)",x="Cyl", y = "Mpg")+
theme_classic()

ggsave('p1.png',height = 10,width = 10,units = 'cm',dpi = 300)


按组更改连续颜色

1
2
3
4
5
6
7
ggplot(df, aes(x=cyl, y=mpg, fill=cyl)) + 
geom_boxplot()+
labs(title="Plot of mpg per cyl (Test data)",x="Cyl", y = "Mpg") +
scale_fill_brewer(palette="Blues") +
theme_bw()

ggsave('p2.png',height = 10,width = 10,units = 'cm',dpi = 300)


分组箱线图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Read the data table
data=read.csv("boxplot_example.csv")
###################
#I.Prepare the data
#1.Normalize the data, etc
for (i in 12:17){
data[,i]=log(data[,i]+1e-3) # log some expression values
}
for (i in 9:17) {
maxValue=max(data[,i]) #scale the data into 0-1
minValue=min(data[,i])
range=maxValue-minValue
data[,i]=(data[,i]-minValue)/range
}
data$X8.Identity=data$X8.Identity/100

#2.Make the new matrix for boxplot: cleaning the data table
library("reshape")
m=melt(data[,c(2,7:12,14:17)], id=1)# remove some columns not to show and reshape the matrix into 3 columns for boxplot drawing in bwplot
colnames(m)=c("Type","Feature","Normalized_Value") #define the new column names

#3.Clean the names of each type and each feature
#Merge sub-types of different elements
m[,1]=sub ("ncRNA_selected","RNAI", m[,1])
m[,1]=sub ("ncRNA_3019","RNAII", m[,1])
m[,1]=sub ("exon_CCDS","CDS", m[,1])
m[,1]=sub ("five_prime_UTR","UTR", m[,1])
m[,1]=sub ("three_prime_UTR","UTR", m[,1])
m[,1]=sub ("ancestral_repeat","AP", m[,1])
#Rename the feature
m[,2]=sub('X7.GC','01.GC Content',m[,2])
m[,2]=sub('X8.Identity','02.DNA Conservation',m[,2])
m[,2]=sub('X9.z_score','03.RNA Struc. Free Energy',m[,2])
m[,2]=sub('X10.SCI','04.RNA Struc. Cons.',m[,2])
m[,2]=sub('X11.tblastx_score','05.Protein Conservation',m[,2])
m[,2]=sub('X12.polyA_RNAseq_MAX','06.PolyA+ RNA-seq',m[,2])
m[,2]=sub('X14.small_RNAseq_MAX','07.Small RNA-seq',m[,2])
m[,2]=sub('X15.Array_totalRNA_MAX','08.Total RNA Array',m[,2])
m[,2]=sub('X16.Array_polyA_MAX','09.PolyA+ RNA Array',m[,2])
m[,2]=sub('X17.Array_nonpolyA_MAX','10.PolyA- RNA Array',m[,2])

###########################
#Making Boxplot
library("lattice")
png("p3.png",width=1500,height=500) # pdf is recommended for most cases, or png for figure with huge amount of data points
#pdf("p3.pdf")
attach(m)
bwplot(Normalized_Value ~ Type|Feature,fill=c("red","green","yellow","blue","light blue"),layout=c(10,1))
dev.off()


带统计检验的箱线图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
data(iris)
print(levels(iris$Species))
comparisons <- list(c("versicolor","setosa"),c("virginica","versicolor"),c("virginica","setosa"))
ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species))+geom_boxplot(alpha = 1, size = 1, position = position_dodge(1.1),outlier.size=0,outlier.alpha = 0)+
geom_point(size = 1, position = position_jitterdodge(dodge.width=0.3,jitter.width = 0.3))+
scale_fill_brewer(palette="Blues") +
theme_bw()+
theme(#legend.position="right",
legend.position="right",
panel.grid=element_blank(),
panel.border=element_blank(),
axis.line = element_line(size=1, colour = "black"),
legend.title = element_text(face="bold", color="black",family = "Arial", size=10),
legend.text= element_text(face="bold", color="black",family = "Arial", size=10),
plot.title = element_text(hjust = 0.5,size=10,face="bold"),
axis.text.x = element_text(face="bold", color="black", size=10,angle = 45,hjust = 1),
axis.text.y = element_text(face="bold", color="black", size=10),
axis.title.x = element_text(face="bold", color="black", size=10),
axis.title.y = element_text(face="bold",color="black", size=10))+
stat_compare_means(comparisons = comparisons,
method = "wilcox.test",
method.args = list(alternative = "greater"),
label = "p.signif"
)+labs(x="",title="Boxplot and statistical test", face="bold")

ggsave('p4.png',height = 10,width = 10,units = 'cm',dpi = 300)


小提琴图

基本小提琴图制作

1
2
3
4
5
6
7
8
9
10
> df$cyl <- as.factor(df$cyl)

> head(df)
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
Valiant 18.1 6 3.460
1
2
3
4
5
ggplot(df, aes(x=cyl, y=mpg)) +
geom_violin(trim=FALSE) +
labs(title="Plot of mpg per cyl (Test data)", x="Cyl", y = "Mpg")

ggsave('p5.png',height = 10,width = 10,units = 'cm',dpi = 300)


在小提琴图上添加汇总统计

添加中位数和四分位数

1
2
3
4
5
6
ggplot(df, aes(x=cyl, y=mpg)) + 
geom_violin(trim=FALSE) +
labs(title="Plot of mpg per cyl (Test data)", x="Cyl", y = "Mpg") +
stat_summary(fun.y=mean, geom="point", shape=23, size=2, color="red")

ggsave('p6.png',height = 10,width = 10,units = 'cm',dpi = 300)


或者

1
2
3
4
5
6
 ggplot(df, aes(x=cyl, y=mpg)) + 
geom_violin(trim=FALSE) +
labs(title="Plot of mpg per cyl (Test data)", x="Cyl", y = "Mpg") +
geom_boxplot(width=0.1)

ggsave('p7.png',height = 10,width = 10,units = 'cm',dpi = 300)


添加均值和标准差

1
2
3
4
5
6
ggplot(df, aes(x=cyl, y=mpg)) + 
geom_violin(trim=FALSE) +
labs(title="Plot of mpg per cyl (Test data)", x="Cyl", y = "Mpg") +
stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="crossbar", width=0.1 )

ggsave('p8.png',height = 10,width = 10,units = 'cm',dpi = 300)


或者

1
2
3
4
5
6
ggplot(df, aes(x=cyl, y=mpg)) + 
geom_violin(trim=FALSE) +
labs(title="Plot of mpg per cyl (Test data)", x="Cyl", y = "Mpg") +
stat_summary(fun.data=mean_sdl, fun.args = list(mult = 1), geom="pointrange", color="red")

ggsave('p9.png',height = 10,width = 10,units = 'cm',dpi = 300)


更改小提琴图填充颜色

1
2
3
4
5
6
7
8
ggplot(df, aes(x=cyl, y=mpg, fill=cyl)) + 
geom_violin(trim=FALSE) +
geom_boxplot(width=0.1, fill="white") +
labs(title="Plot of mpg per cyl", x="Cyl", y = "Mpg") +
scale_fill_brewer(palette="Reds") +
theme_classic()

ggsave('p10.png',height = 10,width = 10,units = 'cm',dpi = 300)


直方图

本R 教程介绍了如何使用 ggplot2 包创建直方图。

使用函数geom_histogram()

1
2
df2 <-read.table("histogram_plots.txt",header=T,sep="\t")
# 测试数据文件可在公众号回复关键词( R语言 )获取

基础直方图制作

1
2
3
4
5
6
7
8
> head(df2)
sex weight
1 F 49
2 F 56
3 F 60
4 F 43
5 F 57
6 F 58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
library(ggplot2)
# 基础直方图
ggplot(df2, aes(x=weight)) + geom_histogram()

ggsave('p11-1.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 改变bins的宽度
ggplot(df2, aes(x=weight)) +
geom_histogram(binwidth=1)

ggsave('p11-2.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 改变颜色
ggplot(df2, aes(x=weight)) +
geom_histogram(color="black", fill="white")

ggsave('p11-3.png',height = 10,width = 10,units = 'cm',dpi = 300)


p11-1,2,3

在直方图上添加平均线和密度图

使用函数geom_vline可以为平均值添加一条线。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 添加平均线
ggplot(df2, aes(x=weight)) +
geom_histogram(binwidth=1, color="black", fill="white") +
geom_vline(aes(xintercept=mean(weight)),
color="red", linetype="dashed", size=1)

ggsave('p12-1.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 添加密度图
ggplot(df2, aes(x=weight)) +
geom_histogram(aes(y=..density..), colour="black", fill="white")+
geom_density(alpha=.2, fill="#87CEEB")

ggsave('p12-2.png',height = 10,width = 10,units = 'cm',dpi = 300)


p12-1,2

更改直方图填充颜色和线的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
# 改变线的颜色和填充颜色
ggplot(df2, aes(x=weight))+
geom_histogram(color="darkblue", fill="lightblue")

ggsave('p13-1.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 改变线的类型

ggplot(df2, aes(x=weight))+
geom_histogram(color="black", fill="lightblue",
linetype="dashed")

ggsave('p13-2.png',height = 10,width = 10,units = 'cm',dpi = 300)


p13-1,2

按组更改直方图颜色

计算每组的平均值:

plyr包用于计算每组的平均weight:

1
2
3
4
5
6
7
library(plyr)
mu <- ddply(df2, "sex", summarise, grp.mean=mean(weight))
head(mu)

sex grp.mean
1 F 54.70
2 M 65.36

更改线条颜色

直方图绘制的线条颜色可以通过变量sex的水平自动控制。

1
2
3
4
5
6
7
8
9
10
# 按组更改直方图的线和填充颜色
ggplot(df2, aes(x=weight, color=sex)) +
geom_histogram(fill="white")

ggsave('p13-3.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 重叠直方图
ggplot(df2, aes(x=weight, color=sex)) +
geom_histogram(fill="white", alpha=0.5, position="identity")

ggsave('p13-4.png',height = 10,width = 10,units = 'cm',dpi = 300)


p13-3,4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 交错直方图
ggplot(df2, aes(x=weight, color=sex)) +
geom_histogram(fill="white", position="dodge")+
theme(legend.position="top")

ggsave('p13-5.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 添加平均线
p <- ggplot(df2, aes(x=weight, color=sex)) +
geom_histogram(fill="white", position="dodge")+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")+
theme(legend.position="top")

ggsave('p13-6.png',height = 10,width = 10,units = 'cm',dpi = 300)


p13-5,6

也可以使用以下函数手动更改直方图绘制线条颜色:

scale_color_manual() : 使用自定义颜色
scale_color_brewer():使用来自 RColorBrewer 包的调色板
scale_color_grey():使用灰色调色板

1
2
3
4
5
6
7
8
9
10
11
12
13
# 使用自定义调色板
p+scale_color_manual(values=c("#87CEEB", "#9ACD32", "#F4A460"))

ggsave('p13-7.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 使用brewer调色板
p+scale_color_brewer(palette="Dark2")

ggsave('p13-8.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 使用灰色
p + scale_color_grey() + theme_classic() +
theme(legend.position="top")

ggsave('p13-9.png',height = 10,width = 10,units = 'cm',dpi = 300)


p13-7,8,9

更改填充颜色

直方图的填充颜色可以由sex自动控制:

1
2
3
4
5
6
7
8
9
10
11
# 按组更改直方图并填充颜色
ggplot(df2, aes(x=weight, fill=sex, color=sex)) +
geom_histogram(position="identity")

ggsave('p13-10.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 使用半透明填充
ggplot(df2, aes(x=weight, fill=sex, color=sex)) +
geom_histogram(position="identity", alpha=0.5)


ggsave('p13-11.png',height = 10,width = 10,units = 'cm',dpi = 300)


p13-10,11

密度图

本R 教程介绍了如何使用 ggplot2包 创建密度图。

使用函数geom_density()

准备数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
head(df)

## sex weight
## 1 F 49
## 2 F 56
## 3 F 60
## 4 F 43
## 5 F 57
## 6 F 58

基本密度图

1
2
3
4
5
6
7
8
9
10
11
12
library(ggplot2)

# 基本密度图
ggplot(df, aes(x=weight)) +
geom_density()

# 添加平均线
ggplot(df, aes(x=weight)) +
geom_density() +
geom_vline(aes(xintercept=mean(weight)),
color="red", linetype="dashed", size=1)

更改密度图线类型和颜色

1
2
3
4
5
6
7
8
# 改变密度图线的类型以及填充颜色
ggplot(df, aes(x=weight))+
geom_density(color="darkblue", fill="lightblue")
ggsave('m3.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 改变线的类型
ggplot(df, aes(x=weight))+
geom_density(linetype="dashed")
ggsave('m4.png',height = 10,width = 12,units = 'cm',dpi = 300)

按组更改密度图颜色

计算每组的平均值

1
2
3
4
5
6
7
library(plyr)
mean <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mean)

## sex grp.mean
## 1 F 54.70
## 2 M 65.36

更改线条颜色

密度图线的颜色可以通过 sex 自动控制:

1
2
3
4
5
6
7
8
9
10
11
12
# 根据分组改变密度图线的颜色
ggplot(df, aes(x=weight, color=sex)) +
geom_density()
ggsave('m5.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 添加平均线
test <- ggplot(df, aes(x=weight, color=sex)) +
geom_density()+
geom_vline(data=mean, aes(xintercept=grp.mean, color=sex),
linetype="dashed")

test
ggsave('m6.png',height = 10,width = 12,units = 'cm',dpi = 300)

也可以使用以下函数手动更改密度图线颜色:

scale_color_manual() : 使用自定义颜色

scale_color_brewer():使用RColorBrewer包的调色板

scale_color_grey():使用灰色调色板

1
2
3
4
5
6
7
8
9
# 使用自定义颜色
test + scale_color_manual(values=c("#1E90FF", "#FF69B4"))
ggsave('m7.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 使用 brewer 调色板
test + scale_color_brewer(palette="Set2")
ggsave('m8.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 使用灰色调色板
test + scale_color_grey() + theme_classic()
ggsave('m9.png',height = 10,width = 12,units = 'cm',dpi = 300)

更改填充颜色

密度图填充颜色可以由 sex 自动控制

1
2
3
4
5
6
7
8
9
10
11
12
13
# 根据分组改变密度图填充颜色
ggplot(df, aes(x=weight, fill=sex)) +
geom_density()
ggsave('m10.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 使用半透明填充
test2<-ggplot(df, aes(x=weight, fill=sex)) +
geom_density(alpha=0.3)
test2
ggsave('m11.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 添加均值线
test2+geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
linetype="dashed")
ggsave('m12.png',height = 10,width = 12,units = 'cm',dpi = 300)

手动更改密度图填充颜色:

1
2
3
4
5
6
7
8
9
# 使用自定义颜色
test2 + scale_fill_manual(values=c("#1E90FF", "#FF69B4"))
ggsave('m13.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 使用 brewer 调色板
test2 + scale_fill_brewer(palette="Set2")
ggsave('m14.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 使用灰色调色板
test2 + scale_fill_grey() + theme_classic()
ggsave('m15.png',height = 10,width = 12,units = 'cm',dpi = 300)

更改图例位置

1
2
3
4
5
6
test2 + theme(legend.position="top")
ggsave('m16.png',height = 10,width = 12,units = 'cm',dpi = 300)
test2 + theme(legend.position="bottom")
ggsave('m17.png',height = 10,width = 12,units = 'cm',dpi = 300)
test2 + theme(legend.position="none") # Remove legend
ggsave('m18.png',height = 10,width = 12,units = 'cm',dpi = 300)

结合直方图和密度图

下面是直方图结合密度图的组合图

1
2
3
4
5
6
7
8
9
10
11
12
13
# 结合直方图和密度图
ggplot(df, aes(x=weight)) +
geom_histogram(aes(y=..density..), colour="darkblue", fill="white")+
geom_density(alpha=.2, fill="#00BFFF")

ggsave('m19.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 分组
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), alpha=0.5,
position="identity")+
geom_density(alpha=.2)

ggsave('m20.png',height = 10,width = 12,units = 'cm',dpi = 300)

点图

本R 教程介绍了如何使用 ggplot2包 创建点图。

使用函数geom_dotplot()

准备数据

使用 ToothGrowth数据集:

1
2
3
4
5
6
7
8
9
10
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)

## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5

基本点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
library(ggplot2)
# 基本点图
p<-ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
p
ggsave('d1.png',height = 10,width = 10,units = 'cm',dpi = 300)


# 更改点大小和堆栈比率
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center',
stackratio=1.5, dotsize=1.2)

ggsave('d2.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 旋转点图
p + coord_flip()
ggsave('d3.png',height = 10,width = 10,units = 'cm',dpi = 300)


自定义选择想要显示的项目,比如只显示 0.52 的点图结果

1
2
3
p + scale_x_discrete(limits=c("0.5", "2"))

ggsave('d4.png',height = 10,width = 10,units = 'cm',dpi = 300)


在点图上添加汇总统计

函数stat_summary()可用于向点图添加均值/中值点等

带箱线图和小提琴图的点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 添加基本的箱线图
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')
ggsave('d5.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 添加带V形切迹的箱线图(Notched boxplot)
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch = TRUE)+
geom_dotplot(binaxis='y', stackdir='center')
ggsave('d6.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 添加小提琴图
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim = FALSE)+
geom_dotplot(binaxis='y', stackdir='center')
ggsave('d7.png',height = 10,width = 10,units = 'cm',dpi = 300)


添加均值和标准差

使用函数 mean_sdlmean_sdl 计算平均值加上或减去一个常数乘以标准偏差

在下面的R代码中,常数是使用参数 mult(mult=1) 指定的。默认情况下,mult=2

平均值 +/- SD 可以添加为 crossbarpointrange

1
2
3
4
5
6
7
8
9
10
11
12
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
geom_dotplot(binaxis='y', stackdir='center')


p + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="crossbar", width=0.5)
ggsave('d8.png',height = 10,width = 10,units = 'cm',dpi = 300)

p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
ggsave('d9.png',height = 10,width = 10,units = 'cm',dpi = 300)


还可以定义一个自定义函数来生成如下汇总统计信息。

1
2
3
4
5
6
7
# 生成汇总统计数据的函数(平均值和 +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}

使用自定义汇总函数:

1
2
3
p + stat_summary(fun.data=data_summary, color="pink")
ggsave('d10.png',height = 10,width = 10,units = 'cm',dpi = 300)


按组更改点图颜色

这一步和之前的直方图以及密度图所用的方法是一样的,这里不再赘述了,可以查看我之前的教程内容。

1
2
3
4
5
6
7
8
9
10
# 使用单一填充颜色
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center', fill="#8A2BE2")
ggsave('d11.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 按组更改点图颜色
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')
p
ggsave('d12.png',height = 10,width = 12,units = 'cm',dpi = 300)


使用以下函数手动更改点图颜色:

scale_fill_manual() : 使用自定义颜色

scale_fill_brewer() :使用来自RColorBrewer包的调色板

scale_fill_grey() :使用灰色调色板

补充一下 RColorBrewer 包所支持的颜色类型名称


1
2
3
4
5
6
7
8
9
10
11
# 使用自定义颜色
p+scale_fill_manual(values=c("#2e2be2", "#8a2be2", "#e22bdf"))
ggsave('d13.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 使用brewer调色板
p+scale_fill_brewer(palette="Dark2")
ggsave('d14.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 使用灰色调色板
p + scale_fill_grey() + theme_classic()
ggsave('d15.png',height = 10,width = 12,units = 'cm',dpi = 300)


更改图例位置

1
2
3
4
5
6
p + theme(legend.position="top")
ggsave('d16.png',height = 10,width = 10,units = 'cm',dpi = 300)
p + theme(legend.position="bottom")
ggsave('d17.png',height = 10,width = 10,units = 'cm',dpi = 300)
p + theme(legend.position="none") # 删除图例
ggsave('d18.png',height = 10,width = 10,units = 'cm',dpi = 300)


更改图例中项目的顺序

函数 scale_x_discrete 可将顺序更改为“2”、“0.5”、“1”:

1
2
p + scale_x_discrete(limits=c("2", "0.5", "1"))
ggsave('d19.png',height = 10,width = 12,units = 'cm',dpi = 300)


多组点图

1
2
3
4
5
6
7
8
9
10
# 按组更改点图颜色
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center')
ggsave('d20.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 更改位置:同一组点图之间的间隔
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.6))
p
ggsave('d21.png',height = 10,width = 12,units = 'cm',dpi = 300)


更改点图颜色并添加箱线图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 更改颜色
p+scale_fill_manual(values=c("#8a2be2", "#dfe22b"))
ggsave('d22.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 添加箱线图
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(fill="white")+
geom_dotplot(binaxis='y', stackdir='center')
ggsave('d23.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 改变位置
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))
ggsave('d24.png',height = 10,width = 12,units = 'cm',dpi = 300)


自定义点图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 基础点图
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
theme_classic()

ggsave('d25.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 按组更改颜色
dp <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
dp + theme_classic()
ggsave('d26.png',height = 10,width = 12,units = 'cm',dpi = 300)


1
2
3
4
5
6
7
8
9
# 连续颜色
dp + scale_fill_brewer(palette="OrRd") + theme_classic()
ggsave('d27.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 离散颜色
dp + scale_fill_brewer(palette="Paired") + theme_minimal()
ggsave('d28.png',height = 10,width = 12,units = 'cm',dpi = 300)
# 渐变色
dp + scale_fill_brewer(palette="PuOr") + theme_minimal()
ggsave('d29.png',height = 10,width = 12,units = 'cm',dpi = 300)


散点图

本文介绍如何使用ggplot2包创建散点图。使用函数·geom_point()

准备数据

以下示例中使用了mtcars数据集

1
2
3
# 将 cyl 列从数值型(numeric)转换为因子变量(factor variable)
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)

基本散点图

使用下面的 R 代码创建简单的散点图。

可以使用函数geom_point()更改点的颜色、大小和形状,如下所示:

1
geom_point(size, color, shape)
1
2
3
4
5
6
7
8
9
library(ggplot2)
# 基本散点图
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave('s01.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 改变点的形状和大小
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
ggsave('s02.png',height = 10,width = 10,units = 'cm',dpi = 300)


点的大小可以由连续变量的值控制,如下例所示。

1
2
3
4
# 改变点的大小
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(size=qsec))
ggsave('s03.png',height = 10,width = 12,units = 'cm',dpi = 300)


在散点图中标记点

可以使用函数geom_text()

1
2
3
4
5
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(label=rownames(mtcars))
ggsave('s04.png',height = 10,width = 12,units = 'cm',dpi = 300)


添加回归线

以下函数可用于向散点图添加回归线:

geom_smooth()
stat_smooth()

geom_abline()

本节仅介绍geom_smooth()函数。

一个简化的格式是:

1
geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95)

方法

要使用的平滑方法。可能的值为 lm、glm、gam、loess、rlm

  • method = “loess”:这是少量观察的默认值。它计算平滑的局部回归。您可以使用 R 代码 ?loess 阅读有关loess 的更多信息。
  • method =“lm”:它适合线性模型。请注意,也可以将公式表示为 formula = y ~ poly(x, 3) 以指定 3 次多项式。
  • se:逻辑值。如果为 TRUE,则置信区间显示在平滑附近。
  • fullrange:逻辑值。如果为 TRUE,则拟合跨越绘图的整个范围
  • level:要使用的置信区间水平。默认值为 0.95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 添加回归线
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm)
ggsave('s05.png',height = 10,width = 10,units = 'cm',dpi = 300)
# 去除置信区间
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, se=FALSE)
ggsave('s06.png',height = 10,width = 10,units = 'cm',dpi = 300)

# Loess method
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth()
ggsave('s07.png',height = 10,width = 10,units = 'cm',dpi = 300)


更改点和线的外观

下面介绍如何更改:

  • 回归线的线型和颜色
  • 置信区间的填充颜色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 更改线型和颜色
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, se=FALSE, linetype="dashed",
color="#FF0000")
ggsave('s08.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 更改置信区间填充颜色
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, linetype="dashed",
color="darkred", fill="#87CEFA")
ggsave('s09.png',height = 10,width = 10,units = 'cm',dpi = 300)


默认情况下,置信带使用透明颜色。可以通过使用参数alpha来改变:geom_smooth(fill="blue", alpha=1)

多组散点图

本节介绍如何自动和手动更改点颜色和形状。

自动更改点颜色/形状/大小

在下面的 R 代码中,点的形状、颜色和大小由因子变量cyl的级别控制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 通过 cyl 的级别更改点形状
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl)) +
geom_point()
ggsave('s10.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 更改点的形状和颜色
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl)) +
geom_point()
ggsave('s11.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 更改点的形状、颜色和大小
ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl, size=cyl)) +
geom_point()
ggsave('s12.png',height = 10,width = 12,units = 'cm',dpi = 300)


添加回归线

可以按如下方式添加回归线:

1
2
3
4
5
6
7
8
9
10
11
12
# 添加回归线
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm)
ggsave('s13.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 删除置信区间,延长回归线
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
ggsave('s14.png',height = 10,width = 12,units = 'cm',dpi = 300)


还可以使用美观的linetype = cyl更改回归线的线型。

置信带的填充颜色可以更改如下:

1
2
3
4
5
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, aes(fill=cyl))
ggsave('s15.png',height = 10,width = 12,units = 'cm',dpi = 300)

手动更改点颜色/形状/大小

使用了以下功能:

  • scale_shape_manual()用于点形状
  • scale_color_manual()用于点颜色
  • scale_size_manual()用于点大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 手动更改点的形状和颜色
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#FFA07A','#87CEFA', '#BA55D3'))+
theme(legend.position="top")
ggsave('s16.png',height = 12,width = 10,units = 'cm',dpi = 300)

# 手动更改点的尺寸
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl))+
geom_point(aes(size=cyl)) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#FFA07A','#87CEFA', '#BA55D3'))+
scale_size_manual(values=c(2,3,4))+
theme(legend.position="top")
ggsave('s17.png',height = 12,width = 10,units = 'cm',dpi = 300)


也可以使用以下功能手动更改点和线颜色:

  • scale_color_brewer():使用来自RColorBrewer包的调色板
  • scale_color_grey():使用灰色调色板
1
2
3
4
5
6
7
8
9
10
11
12
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
theme_classic()
# Use brewer color palettes
p+scale_color_brewer(palette="Dark2")
ggsave('s18.png',height = 10,width = 12,units = 'cm',dpi = 300)

# Use grey scale
p + scale_color_grey()
ggsave('s19.png',height = 10,width = 12,units = 'cm',dpi = 300)


将联合分布图添加到散点图

可以使用函数geom_rug()

1
geom_rug(sides ="bl")

side:一个字符串,用于控制出现在哪一边。允许的值是一个字符串,其中包含 trbl 中的任何一个,用于顶部、右侧、底部和左侧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 添加联合分布图(marginal rugs)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + geom_rug()
ggsave('s20.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 更改颜色
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() + geom_rug()
ggsave('s21.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 使用可靠的数据添加联合分布图
ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point() + geom_rug()
ggsave('s22.png',height = 10,width = 10,units = 'cm',dpi = 300)


具有 2D 密度评估的散点图

可以使用函数geom_density_2d()stat_density_2d()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 具有二维密度估计的散点图
sp <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point()
sp + geom_density_2d()
ggsave('s23.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 渐变色
sp + stat_density_2d(aes(fill = ..level..), geom="polygon")
ggsave('s24.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 更改渐变颜色
sp + stat_density_2d(aes(fill = ..level..), geom="polygon")+
scale_fill_gradient(low="blue", high="red")
ggsave('s25.png',height = 10,width = 12,units = 'cm',dpi = 300)


带椭圆的散点图

函数stat_ellipse()可以如下使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 一个椭圆环绕所有点
ggplot(faithful, aes(waiting, eruptions))+
geom_point()+
stat_ellipse()
ggsave('s26.png',height = 10,width = 10,units = 'cm',dpi = 300)

# 椭圆分组
p <- ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3))+
geom_point()
p + stat_ellipse()
ggsave('s27.png',height = 10,width = 12,units = 'cm',dpi = 300)

# 更改椭圆的类型:可能的值为“t”、“norm”、“euclid”
p + stat_ellipse(type = "t")
ggsave('s28.png',height = 10,width = 12,units = 'cm',dpi = 300)


多重相关图

1
2
3
4
5
6
7
data(iris)
library(Hmisc)
library(corrplot)
res2 <- rcorr(as.matrix(iris[c("Sepal.Width","Petal.Length","Petal.Width")]))
corrplot(corr = res2$r,tl.col="black",type="lower", order="original",tl.pos = "d",tl.cex=1.0,
p.mat = res2$P, sig.level = 0.05,insig = "blank")


火山图

1
2
3
4
5
6
7
8
> head(df3)
Gene log2FoldChange pvalue padj
1 DOK6 0.5100 1.861e-08 0.0003053
2 TBX5 -2.1290 5.655e-08 0.0004191
3 SLC32A1 0.9003 7.664e-08 0.0004191
4 IFITM1 -1.6870 3.735e-06 0.0068090
5 NUP93 0.3659 3.373e-06 0.0068090
6 EMILIN2 1.5340 2.976e-06 0.0068090
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
df3$threshold <- as.factor(ifelse(df3$padj < 0.05 & abs(df3$log2FoldChange) >=1,ifelse(df3$log2FoldChange > 1 ,'Up','Down'),'Not'))
ggplot(data=df3, aes(x=log2FoldChange, y =-log10(padj), color=threshold,fill=threshold)) +
scale_color_manual(values=c("blue", "grey","red"))+
geom_point(size=1) +
xlim(c(-3, 3)) +
theme_bw(base_size = 12, base_family = "Times") +
geom_vline(xintercept=c(-1,1),lty=4,col="grey",lwd=0.6)+
geom_hline(yintercept = -log10(0.05),lty=4,col="grey",lwd=0.6)+
theme(legend.position="right",
panel.grid=element_blank(),
legend.title = element_blank(),
legend.text= element_text(face="bold", color="black",family = "Times", size=8),
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(face="bold", color="black", size=12),
axis.text.y = element_text(face="bold", color="black", size=12),
axis.title.x = element_text(face="bold", color="black", size=12),
axis.title.y = element_text(face="bold",color="black", size=12))+
labs(x="log2FoldChange",y="-log10 (adjusted p-value)",title="Volcano plot of DEG", face="bold")

ggsave('p26.png',height = 10,width = 10,units = 'cm',dpi = 300)


曼哈顿图

1
2
3
4
5
6
7
8
> head(df4)
SNP CHR BP P
1 rs1 1 1 0.9148060
2 rs2 1 2 0.9370754
3 rs3 1 3 0.2861395
4 rs4 1 4 0.8304476
5 rs5 1 5 0.6417455
6 rs6 1 6 0.5190959
1
2
3
4
manhattan(df4, main = "GWAS results", ylim = c(0, 8),
cex = 0.5, cex.axis=0.8, col=c("dodgerblue4","deepskyblue"),
#suggestiveline = F, genomewideline = F, #remove the suggestive and genome-wide significance lines
chrlabs = as.character(c(1:22)))


热图

使用ggplots包heatmap.2()函数绘制热图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> head(dm)
Control1 Tumor2 Control3 Tumor4 Control5 Tumor1 Control2
Gene1 3.646058 -0.98990248 2.210404 -0.2063050 2.859744 1.3304284 2.690376
Gene2 4.271172 -1.16217765 2.734119 -2.4782173 3.752013 0.0255639 4.471795
Gene3 3.530448 1.11451101 1.635485 -0.4241215 3.701427 1.2263312 3.588787
Gene4 3.061122 -1.18791027 4.331229 0.8733314 2.349352 0.4825479 1.854433
Gene5 1.956817 0.25431042 1.984438 1.2713845 1.685917 1.4554739 2.445830
Gene6 2.000919 0.06015972 4.480901 0.9780682 3.063475 -0.4222994 3.585366
Tumor3 Control4 Tumor5
Gene1 0.6135943 2.470413 0.5158246
Gene2 1.6516242 2.735508 -0.5837784
Gene3 -0.6349656 1.999844 0.1417349
Gene4 -1.2237684 1.154377 -0.9301261
Gene5 0.3316909 2.715163 0.1866400
Gene6 1.0689000 2.563422 1.3465830
1
2
3
4
5
6
7
8
9
10
11
12
13
##为了以红色绘制高表达式值,在热图中使用colorRampPalette而不是heatmap.2
##colorRampPalette是RColorBrewer包中的一个函数
cr <- colorRampPalette(c("red","white","blue"))
heatmap.2(dm,
scale="row", #scale the rows, scale each gene's expression value
key=T, keysize=1.1,
cexCol=0.9,cexRow=0.8,
col=cr(1000),
ColSideColors=c(rep(c("red","blue"),5)),
density.info="none",trace="none",
#dendrogram='none', #如果要删除树状图解除此注释
Colv = T,Rowv = T) #按行和列进行聚类


使用pheatmap包的pheatmap函数绘制热图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
##add column and row annotations
annotation_col = data.frame(CellType = factor(rep(c("Control", "Tumor"), 5)), Time = 1:5)
rownames(annotation_col) = colnames(dm)
annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
##set colors of each group
ann_colors = list(Time = c("white", "springgreen4"),
CellType = c(Control = "#6B8E23", Tumor = "#DA70D6"),
GeneClass = c(Path1 = "#85e5e5", Path2 = "#afeeee", Path3 = "#d9f7f7"))
##draw the heatmap
pheatmap(dm,
cutree_col = 2, cutree_row = 3, #break up the heatmap by clusters you define
cluster_rows=TRUE, show_rownames=TRUE, cluster_cols=TRUE, #by default, pheatmap clusters by both row and col
annotation_col = annotation_col, annotation_row = annotation_row,annotation_colors = ann_colors)


使用ggplot2软件包绘制热图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
##9.3.1.cluster by row and col
##cluster and re-order rows
rowclust = hclust(dist(dm))
reordered = dm[rowclust$order,]
##cluster and re-order columns
colclust = hclust(dist(t(dm)))
##9.3.2.scale each row value in [0,1]
dm.reordered = reordered[, colclust$order]
dm.reordered=apply(dm.reordered,1,rescale) #rescale is a function in the scales package
dm.reordered=t(dm.reordered) #transposed matrix
##9.3.3.save col and row names before changing the matrix format
col_name=colnames(dm.reordered)
row_name=rownames(dm.reordered)
##9.3.4.change data format for geom_title
colnames(dm.reordered)=1:ncol(dm.reordered)
rownames(dm.reordered)=1:nrow(dm.reordered)
dm.reordered=melt(dm.reordered) #melt is a function in the reshape2 package
head(dm.reordered)
##9.3.5.draw the heatmap
ggplot(dm.reordered, aes(Var2, Var1)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_gradient(low = "white", high = "steelblue") +
theme_grey(base_size = 6) +
labs(x = "", y = "") +
scale_x_continuous(expand = c(0, 0),labels=col_name,breaks=1:length(col_name)) +
scale_y_continuous(expand = c(0, 0),labels=row_name,breaks=1:length(row_name))

ggsave('p30.png',height = 10,width = 15,units = 'cm',dpi = 300)


气球图

基础气球图制作

1
2
3
4
5
6
7
8
> head(df6)
Biological.process Fold.enrichment X.log10.Pvalue. col
1 Small molecule metabolic process 1.0 16 1
2 Single-organism catabolic process 1.5 12 1
3 Oxoacid metabolic process 2.0 23 1
4 Small molecule biosynthetic process 2.5 6 1
5 Carboxylic acid metabolic process 2.7 24 1
6 Organic acid metabolic process
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ggplot(df6, aes(x=Fold.enrichment, y=Biological.process)) +
geom_point(aes(size = X.log10.Pvalue.)) +
scale_x_continuous(limits=c(0,7),breaks=0:7) +
scale_size(breaks=c(1,5,10,15,20,25)) +
theme_light() +
theme(panel.border=element_rect(fill='transparent', color='black', size=1),
plot.title = element_text(color="black", size=12, hjust=0.5, face="bold", lineheight=1),
axis.title.x = element_text(color="black", size=8, face="bold"),
axis.title.y = element_text(color="black", size=10, vjust=1.5, face="bold"),
axis.text.x = element_text(size=10,color="black",face="bold"),
axis.text.y = element_text(size=10,color="black",face="bold"),
legend.text = element_text(color="black", size=10, hjust=-2),
legend.position="bottom") +
labs(x="Fold Enrichment",y="Biological Process",size="-log10(Pvalue)", title="GO Enrichment (Test data)",face="bold")

ggsave('p31.png',height = 10,width = 15,units = 'cm',dpi = 300)


改变点的颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ggplot(df6, aes(x=col, y=Biological.process,color=X.log10.Pvalue.)) +
geom_point(aes(size = Fold.enrichment)) +
scale_x_discrete(limits=c("1")) +
scale_size(breaks=c(1,2,4,6)) +
scale_color_gradient(low="#fcbba1", high="#a50f15") +
theme_classic() +
theme(panel.border=element_rect(fill='transparent', color='black', size=1),
plot.title = element_text(color="black", size=12, hjust=0.5, face="bold", lineheight=1),
axis.title.x = element_blank(),
axis.title.y = element_text(color="black", size=10, face="bold"),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(size=10,color="black",face="bold"),
legend.text = element_text(color="black", size=8)) +
labs(y="Biological Process",size="Fold Enrichment", color="-Log10(Pvalue)",title="GO Enrichment (Test data)",face="bold")

ggsave('p32.png',height = 10,width = 16,units = 'cm',dpi = 300)


组合条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library(reshape2)
#build example matrix
mat <- as.data.frame(list(c(100,10,1),c(3,6,100)))
colnames(mat) <- c("a","b")
rownames(mat) <- c("gene1","gene2","gene3")
plot <- melt(mat)
plot$gene <- rep(c("gene1","gene2","gene3"),ncol(mat))
colnames(plot) <- c("sample","value","gene")

#barplot
library(ggpubr)
plot_a <- plot[plot$sample=="a",]
plot_b <- plot[plot$sample=="b",]
barplot_theme <- function(){
theme(
plot.margin = unit(x=c(10,5,0,5),units="pt"),
legend.position="null",
panel.grid=element_blank(),
panel.border=element_blank(),
axis.line.y = element_line(color = "black",size = 1.5),
axis.ticks.y = element_line(color = "black",size = 1.5),
axis.ticks.x = element_blank(),
legend.title = element_text(face="bold", color="black",family = "Arial", size=20),
legend.text= element_text(face="bold", color="black",family = "Arial", size=20),
plot.title = element_text(hjust = 0.5,size=24,face="bold"),
axis.text.x = element_blank(),
#axis.text.x = element_text(face="bold", color="black", size=20, angle = 90,hjust = 0,vjust = 0.5),
axis.text.y = element_text(face="bold", color="black", size=18, angle = 90,hjust=0.5),
axis.title.x = element_text(face="bold", color="black", size=24),
axis.title.y = element_text(face="bold",color="black", size=24))
}
p_a <- ggplot(plot_a,aes(x=gene,y=value,fill=sample))+geom_bar(stat = "identity",color = "black",size = 1.2)+
theme_bw()+
xlab("")+
ylab("value: a")+
barplot_theme()+
scale_fill_manual(values=c("blue"))

p_b <- ggplot(plot_b,aes(x=gene,y=value,fill=sample))+geom_bar(stat = "identity",color = "black", size= 1.2)+
theme_bw()+
scale_y_reverse()+
xlab("")+
ylab("value: b")+
barplot_theme()+
scale_fill_manual(values = c("red"))

#plot combination
ggarrange(p_a, p_b,
ncol = 1, nrow = 2,heights = 5,align = c("v"))

ggsave('p33.png',height = 10,width = 10,units = 'cm',dpi = 300)


堆积条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#build example matrix
fraction <- as.data.frame(list(c(0.2,0.3,0.5),c(0.1,0.7,0.2),c(0.8,0.1,0.1)))
colnames(fraction) <- c("a","b","c")
rownames(fraction) <- c("componentA","componentB","componentC")
fraction
stackplot <- melt(fraction)
stackplot$component <- rep(c("componentA","componentB","componentC"),ncol(fraction))
colnames(stackplot) <- c("sample","fraction","component")

#stackplot
ggplot(stackplot,aes(x=sample,y=fraction*100,fill = component)) + geom_bar(stat = "identity", width=0.5, col='black') +
theme_bw()+
theme(#legend.position="bottom",
legend.position="right",
panel.grid=element_blank(),
legend.title = element_text(face="bold", color="black",family = "Arial", size=20),
legend.text= element_text(face="bold", color="black",family = "Arial", size=20),
plot.title = element_text(hjust = 0.5,size=24,face="bold"),
axis.text.x = element_text(face="bold", color="black", size=20,angle = 90,hjust = 1,vjust =0.5),
axis.text.y = element_text(face="bold", color="black", size=20),
axis.title.x = element_text(face="bold", color="black", size=24),
axis.title.y = element_text(face="bold",color="black", size=24))+
ylab("Fraction(%)")+
xlab("")+
#geom_vline(aes(xintercept=6.5))+
scale_y_continuous(breaks = c(0,25,50,75,100),labels = c("0","25","50","75","100"),expand = c(0,0),limits = c(0,103))
scale_fill_aaas(alpha = 1)

ggsave('p34.png',height = 12,width = 15,units = 'cm',dpi = 300)