歡迎關(guān)注天下博客:http://blog.genesino.com/2017/08/barplot-r/
Jump to...
柱狀圖繪制
柱狀圖也是較為常見的一種數(shù)據(jù)展示方式,可以展示基因的表達(dá)量,也可以展示GO富集分析結(jié)果,基因注釋數(shù)據(jù)等。39個(gè)轉(zhuǎn)錄組分析工具,120種組合評(píng)估(轉(zhuǎn)錄組分析工具哪家強(qiáng)-導(dǎo)讀版)中提到了較多堆積柱狀圖的使用。下面就詳細(xì)介紹下怎么繪制。
常規(guī)矩陣柱狀圖繪制
有如下4個(gè)基因在5組樣品中的表達(dá)值
data_ori <- "Grp_1;Grp_2;Grp_3;Grp_4;Grp_5
a;2.6;2.9;2.1;2.0;2.2
b;20.8;9.8;7.0;3.7;19.2
c;10.0;11.0;9.2;12.4;9.6
d;9;3.3;10.3;11.1;10"
data <- read.table(text=data_ori, header=T, row.names=1, sep=";", quote="")
data
Grp_1 Grp_2 Grp_3 Grp_4 Grp_5
a 2.6 2.9 2.1 2.0 2.2
b 20.8 9.8 7.0 3.7 19.2
c 10.0 11.0 9.2 12.4 9.6
d 9.0 3.3 10.3 11.1 10.0
整理數(shù)據(jù)格式,保留基因名字信息
library(ggplot2)
library(reshape2)
library(dplyr)
data_rownames <- rownames(data)
data_colnames <- colnames(data)
data$gene <- data_rownames
data_m <- melt(data, id.vars=c("gene"))
data_m
gene variable value
1 a Grp_1 2.6
2 b Grp_1 20.8
3 c Grp_1 10.0
4 d Grp_1 9.0
5 a Grp_2 2.9
6 b Grp_2 9.8
7 c Grp_2 11.0
8 d Grp_2 3.3
首先看下每個(gè)基因在不同組的表達(dá)情況
# 給定數(shù)據(jù),和x軸、y軸所在列名字
# 直接使用geom_bar就可以繪制柱狀圖
# position: dodge: 柱子并排放置
p <- ggplot(data_m, aes(x=gene, y=value))
p + geom_bar(stat="identity", position="dodge", aes(fill=variable))
# 如果沒有圖形界面,運(yùn)行下面的語句把圖存在工作目錄下的Rplots.pdf文件中
#dev.off()

柱子有點(diǎn)多,也可以利用mean±SD的形式展現(xiàn)
首先計(jì)算平均值和標(biāo)準(zhǔn)差,使用group_by按gene分組,對(duì)每組做summarize
# 獲取平均值和標(biāo)準(zhǔn)差
data_m_sd_mean <- data_m %>% group_by(gene) %>% dplyr::summarise(sd=sd(value), value=mean(value))
data_m_sd_mean <- as.data.frame(data_m_sd_mean)
data_m_sd_mean
gene sd value
1 a 0.3781534 2.36
2 b 7.5491721 12.10
3 c 1.2837445 10.44
4 d 3.1325708 8.74
使用geom_errorbar添加誤差線
p <- ggplot(data_m_sd_mean, aes(x=gene, y=value)) +
geom_bar(stat="identity") +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd))
p

設(shè)置誤差線的寬度和位置
p <- ggplot(data_m_sd_mean, aes(x=gene, y=value)) +
geom_bar(stat="identity", aes(fill=gene)) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd), width=0.2, position=position_dodge(width=0.75))
p

每個(gè)基因的原始表達(dá)值堆積柱狀圖 (只需要修改positon=stack)
# position="fill" 展示的是堆積柱狀圖各部分的相對(duì)比例
# position="stack" 展示的是堆積柱狀圖的原始值
p <- ggplot(data_m, aes(x=variable, y=value)) +
geom_bar(stat="identity", position="stack", aes(fill=gene)) +
geom_text(aes(label=value), position=position_stack(vjust=0.5))
p
堆積柱狀圖顯示沒問題,但文本標(biāo)記錯(cuò)位了

指定下分組信息,位置計(jì)算就正確了
# position="fill" 展示的是堆積柱狀圖各部分的相對(duì)比例
# position="stack" 展示的是堆積柱狀圖的原始值
p <- ggplot(data_m, aes(x=variable, y=value, group=gene)) +
geom_bar(stat="identity", position="stack", aes(fill=gene)) +
geom_text(aes(label=value), position=position_stack(vjust=0.5))
p

比較每組各個(gè)基因的相對(duì)表達(dá) (position=fill)
# position="fill" 展示的是堆積柱狀圖各部分的相對(duì)比例
# position="stack" 展示的是堆積柱狀圖的原始值,可以自己體現(xiàn)下看卡差別
p <- ggplot(data_m, aes(x=variable, y=value)) +
geom_bar(stat="identity", position="fill", aes(fill=gene))
p

縱軸的顯示改為百分比
p <- ggplot(data_m, aes(x=variable, y=value)) +
geom_bar(stat="identity", position="fill", aes(fill=gene)) +
scale_y_continuous(labels = scales::percent)
p

在柱子中標(biāo)記百分比值
首先計(jì)算百分比,同樣是group_by (按照給定的變量分組,然后按組操作)和mutate兩個(gè)函數(shù)(在當(dāng)前數(shù)據(jù)表增加新變量)
# group_by: 按照給定的變量分組,然后按組操作
# mutate: 在當(dāng)前數(shù)據(jù)表增加新變量
# 第一步增加每個(gè)組的加和,第二步計(jì)算比例
data_m <- data_m %>% group_by(variable) %>% mutate(count=sum(value)) %>% mutate(freq=round(100*value/count,2))
再標(biāo)記相對(duì)比例信息
p <- ggplot(data_m, aes(x=variable, y=value, group=gene)) +
geom_bar(stat="identity", position="fill", aes(fill=gene)) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(label=freq), position=position_fill(vjust=0.5))
p

長(zhǎng)矩陣分面繪制
再復(fù)雜一些的矩陣 (除了有不同時(shí)間點(diǎn)的信息,再增加對(duì)照和處理的信息)
library(ggplot2)
library(reshape2)
library(dplyr)
data_ori <- "Gene;Group;Expr;Condition
a;T1;2.6;Control
b;T1;20.8;Control
c;T1;10;Control
d;T1;9;Control
a;T2;2.9;Control
b;T2;9.8;Control
c;T2;11;Control
d;T2;3.3;Control
a;T3;2.1;Control
b;T3;7;Control
c;T3;9.2;Control
d;T3;10.3;Control
a;T4;2;Control
b;T4;3.7;Control
c;T4;12.4;Control
d;T4;11.1;Control
a;T5;2.2;Control
b;T5;19.2;Control
c;T5;9.6;Control
d;T5;10;Control
d;T1;2.6;Treatment
b;T1;20.8;Treatment
c;T1;10;Treatment
a;T1;9;Treatment
d;T2;2.9;Treatment
b;T2;9.8;Treatment
c;T2;11;Treatment
a;T2;3.3;Treatment
a;T3;2.1;Treatment
c;T3;7;Treatment
b;T3;9.2;Treatment
d;T3;10.3;Treatment
a;T4;2;Treatment
c;T4;3.7;Treatment
b;T4;12.4;Treatment
d;T4;11.1;Treatment
a;T5;2.2;Treatment
d;T5;19.2;Treatment
c;T5;9.6;Treatment
b;T5;10;Treatment"
data_m <- read.table(text=data_ori, header=T, sep=";", quote="")
head(data_m)
Gene Group Expr Condition
1 a T1 2.6 Control
2 b T1 20.8 Control
3 c T1 10.0 Control
4 d T1 9.0 Control
5 a T2 2.9 Control
6 b T2 9.8 Control
首先看下每個(gè)基因在不同組的表達(dá)情況, facet_grid和facet_wrap可以對(duì)圖形分面顯示。
# scales: free_y 表示不同子圖之間使用獨(dú)立的Y軸信息
# 但x軸使用同樣的信息。
# 其它可選參數(shù)有free_x, free, fixed
p <- ggplot(data_m, aes(x=Gene, y=Expr)) +
geom_bar(stat="identity", position="dodge", aes(fill=Group)) +
facet_grid(Condition~., scales="free_y")
p
# 如果沒有圖形界面,運(yùn)行下面的語句把圖存在工作目錄下的Rplots.pdf文件中
#dev.off()

柱子有點(diǎn)多,也可以利用mean±SD的形式展現(xiàn)
# 獲取平均值和標(biāo)準(zhǔn)差
# 分組時(shí)不只Gene一個(gè)變量了,還需要考慮Condition
data_m_sd_mean <- data_m %>% group_by(Gene, Condition) %>% dplyr::summarise(sd=sd(Expr), value=mean(Expr))
data_m_sd_mean <- as.data.frame(data_m_sd_mean)
data_m_sd_mean
Gene Condition sd value
1 a Control 0.3781534 2.36
2 a Treatment 2.9978326 3.72
3 b Control 7.5491721 12.10
4 b Treatment 4.8299068 12.44
5 c Control 1.2837445 10.44
6 c Treatment 2.9458445 8.26
7 d Control 3.1325708 8.74
8 d Treatment 6.8568943 9.22
p <- ggplot(data_m_sd_mean, aes(x=Gene, y=value)) +
geom_bar(stat="identity", aes(fill=Gene)) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd), width=0.2, position=position_dodge(width=0.75)) +
facet_wrap(~Condition, ncol=1)
p

每組里面各個(gè)基因的相對(duì)表達(dá), 縱軸的顯示改為百分比
# position="fill" 展示的是堆積柱狀圖各部分的相對(duì)比例
# position="stack" 展示的是堆積柱狀圖的原始值,可以自己體現(xiàn)下看卡差別
p <- ggplot(data_m, aes(x=Group, y=Expr)) +
geom_bar(stat="identity", position="fill", aes(fill=Gene)) +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~Condition, ncol=1)
p
facet后,顯示正常,不需要做特別的修改

在柱子中標(biāo)記百分比值 (計(jì)算百分比值需要注意了, 文本顯示位置還是跟之前一致)
# group_by: 按照給定的變量分組,然后按組操作
# mutate: 在當(dāng)前數(shù)據(jù)表增加新變量
# 第一步增加每個(gè)組 (Group和Condition共同定義分組)的加和,第二步計(jì)算比例
data_m <- data_m %>% group_by(Group, Condition) %>% mutate(count=sum(Expr)) %>% mutate(freq=round(100*Expr/count,2))
p <- ggplot(data_m, aes(x=Group, y=Expr, group=Group)) +
geom_bar(stat="identity", position="fill", aes(fill=Gene)) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(label=freq), position=position_fill(vjust=0.5)) +
facet_wrap(~Condition, ncol=1)
p
文本顯示位置沒有問題,但柱子的位置有些奇怪,使得兩組之間不可比。

先對(duì)數(shù)據(jù)做下排序,然后再標(biāo)記文本
# with: 產(chǎn)生一個(gè)由data_m組成的局部環(huán)境,再這個(gè)環(huán)境里,列名字可以直接使用
data_m <- data_m[with(data_m, order(Condition, Group, Gene)),]
p <- ggplot(data_m, aes(x=Group, y=Expr, group=Group)) +
geom_bar(stat="identity", position="fill", aes(fill=Gene)) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(label=freq), position=position_fill(vjust=0.5)) +
facet_wrap(~Condition, ncol=1)
p
這樣兩種條件下的比較更容易了

生信寶典,一起換個(gè)角度學(xué)生信
<footer class="entry-meta" style="box-sizing: border-box; display: block; font-size: 0.75rem; text-transform: uppercase; color: rgba(187, 187, 187, 0.8); margin: 50px 30px 30px; text-align: center; font-family: Lato, Calibri, Arial, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">RBIOINFOCHENTONG
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處。


</footer>