圖片來源:Husquin L T, Rotival M, Fagny M, et al. Exploring the genetic basis of human population differences in DNA methylation and their causal impact on immune gene regulation[J]. Genome biology, 2018, 19(1): 222.
這里做b, c, d 圖榆鼠。先定義好theme。
theme = theme_bw() + #去掉背景灰色
theme(
panel.background = element_rect(fill="white"), #背景顏色
panel.grid.minor = element_line(NA), panel.grid.major= element_line(NA),#去掉次級網(wǎng)格線亥鸠,x軸網(wǎng)格線
axis.title=element_text(size=14),#坐標(biāo)軸標(biāo)題大小
axis.text=element_text(size=10,colour = "black"), #坐標(biāo)軸文字大小
axis.line=element_line(colour="black")
)
B圖:帶“bar”的柱形圖
Notes:
1.顏色使用近似的“darkred”和“darkblue”;
2.橫坐標(biāo)標(biāo)注按指定順序排列:一般默認(rèn)按字符排序妆够,不符合的話需要排序處理下;
- 坐標(biāo)實際為以 y=1 為起始點,y>1的向上,y<1的朝下责静。作圖時一般以0為界,>0 朝上盖桥,<0 朝下灾螃,這里可以在作圖時使用 y-1,然后重新繪制y軸(坐標(biāo)軸+1)即可揩徊。
d<-read.table("example",sep="\t",header=TRUE)
head(d)
# Feature value errorbar_min errorbar_max Group
#1 TSS1500 0.4731375 0.3605666 3.696081 A1
#2 TSS200 1.5050162 0.8251736 2.965738 A1
#3 5UTR 1.0052282 0.8942654 2.433920 A1
newFeature=factor(d[,1],levels=c("TSS1500","TSS200","5UTR","1stExon","Body","3UTR","Enhancer","Promoter"))
df<-data.frame(newFeature,d)
dd=with(df,df[which(Group %in% c("A1","A2")),])
#dd$value<-dd$value-1
p = ggplot(dd,aes(x=newFeature,y=value-1,fill=Group)) + #將y-1
geom_bar(stat="identity",width=0.5,position = position_dodge()) + #設(shè)置柱子寬度
geom_errorbar(aes(ymax=OR_max-1,ymin=OR_min-1), #將y-1
width = 0.5, #設(shè)置誤差線寬度
position = position_dodge2(padding=0.6)) +
labs(x = "Feature",y="value") +
scale_fill_manual(values=c("darkred","darkblue")) +
scale_y_continuous(limits=c(-1,10), breaks=seq(-1,10,1),labels=c(0:11)) #重新設(shè)置y軸的坐標(biāo)軸值
p + theme+ geom_hline(aes(yintercept=0), colour="black") #增加一條水平線
C圖:餅圖和密度線圖
#餅圖
t<-c(54,45)
pie(t,col = c("darkred","darkblue"),labels = "") #會自動計算百分比
#密度線圖
d = read.table("density_example",header=TRUE)
head(d) #group列有A,B,C...類
# value group
#1 0.303772 A
#2 0.380327 A
#3 0.876526 A
#4 0.957544 A
#5 0.892705 A
#6 0.308518 A
dd <- subset(d,d$group==c("A","B")) #提取子集
p = ggplot(dd,aes(value,colour=group)) +
geom_line(stat="density") +
scale_color_manual(values=c("darkred","darkblue")) + #手動設(shè)置顏色
scale_x_continuous(limits=c(0,1),expand=c(0,0))
#x/y不是從0開始腰鬼,可根據(jù)數(shù)據(jù)類型按以下設(shè)置為從0開始
#scale_y_continuous(expand=c(0,0))
#scale_x_continuous(expand=c(0,0))
#離散型scale_y_discrete 或者scale_x_discrete
p + theme
d圖:橫向柱形圖
柱子右邊的數(shù)字就手動添加吧。
require(ggplot2)
require(stringr)
d<-read.table("example",sep="\t")
head(d,2)
# V1 V2
#1 integral component of lumenal side of endoplasmic reticulum membrane 7.105958
#2 lumenal side of endoplasmic reticulum membrane 6.188589
d$newV1=str_wrap(d$V1,width=10) #newV1:giving target line width; long value will be displayed in more than one lines
p = ggplot(d,aes(x = reorder(newV1, V2),y=V2)) + #newV1按V2排序后顯示
geom_bar(stat="identity",col="black", #“identity”即不做任何計算塑荒,顯示原有數(shù)值熄赡;col設(shè)置邊框顏色
fill="darkred",width=0.5) + #fill 柱子的填充色;width 柱子寬度(0-1范圍內(nèi))
coord_flip() + #旋轉(zhuǎn)坐標(biāo)
labs(x="",y="-log10(P value)") #設(shè)置橫縱坐標(biāo)名稱
p + theme