做b,c,d
图片来源: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),#去掉次级网格线,x轴网格线
axis.title=element_text(size=14),#坐标轴标题大小
axis.text=element_text(size=10,colour = "black"), #坐标轴文字大小
axis.line=element_line(colour="black")
)
B图:带“bar”的柱形图
barplot with errorbar
Notes:
1.颜色使用近似的“darkred”和“darkblue”;
2.横坐标标注按指定顺序排列:一般默认按字符排序,不符合的话需要排序处理下;
- 坐标实际为以 y=1 为起始点,y>1的向上,y<1的朝下。作图时一般以0为界,>0 朝上,<0 朝下,这里可以在作图时使用 y-1,然后重新绘制y轴(坐标轴+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()) + #设置柱子宽度
geom_errorbar(aes(ymax=OR_max-1,ymin=OR_min-1), #将y-1
width = 0.5, #设置误差线宽度
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)) #重新设置y轴的坐标轴值
p + theme+ geom_hline(aes(yintercept=0), colour="black") #增加一条水平线
C图:饼图和密度线图
pie-example.png
密度线图
#饼图
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")) + #手动设置颜色
scale_x_continuous(limits=c(0,1),expand=c(0,0))
#x/y不是从0开始,可根据数据类型按以下设置为从0开始
#scale_y_continuous(expand=c(0,0))
#scale_x_continuous(expand=c(0,0))
#离散型scale_y_discrete 或者scale_x_discrete
p + theme
d图:横向柱形图
柱子右边的数字就手动添加吧。
横向柱形图
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”即不做任何计算,显示原有数值;col设置边框颜色
fill="darkred",width=0.5) + #fill 柱子的填充色;width 柱子宽度(0-1范围内)
coord_flip() + #旋转坐标
labs(x="",y="-log10(P value)") #设置横纵坐标名称
p + theme