1.基本频数分布直方图
library(ggplot2)
library(patchwork)
设置配色:
install.packages("MetBrewer")
isfahan <- MetBrewer::met.brewer("Isfahan1")
length(isfahan)#八种颜色
isfahan[1]
1.ggplot直方图绘制
ggplot()+
geom_histogram(data = mydata,
bins = 50,
aes(x=GRS,weight=GRS),
fill=colorspace::lighten(isfahan[2],0.35),
color="white")
fill:可以直接填充其他喜欢的颜色,weight表示需要计算的频数。
将weigt去掉,即可
2.基本概率密度分布图
ggplot(data=mydata,aes(x=GRS))+
geom_histogram(bins = 50,
aes(y=..density..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_density(alpha=0.3,fill="lightblue",color="red",lwd=1)
y=..density..设置为密度图,geom_density设置密度线,alpha不透明度,fill填充的颜色,color线条颜色,lwd设置线宽度,lty设置线的类型。
3. 上下倒影直方图
ggplot()+
geom_histogram(data=mydata,
bins = 50,
aes(x=GRS),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_histogram(data = mydata,
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[6],0.35),#填充颜色
color="white")
1. 设置坐标轴范围
ggplot()+
geom_histogram(data=mydata,
bins = 50,
aes(x=GRS),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_histogram(data = mydata,
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[6],0.35),#填充颜色
color="white")+
coord_cartesian(xlim=c(-3,3))
2.按照某条件分组(如性别)绘制倒影图
利用filter函数提取数据框mydata中的某一亚组
ggplot()+
geom_histogram(data=filter(mydata,Sex=="Female"),
bins = 50,
aes(x=GRS),
binwidth = 0.2,
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_histogram(data = filter(mydata,Sex=="Male"),
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[6],0.35),#填充颜色
color="white")
3.叠加条件,比如叠加发生AF结局的患者
ggplot()+
geom_histogram(data=filter(mydata,Sex=="Female"),
bins = 50,
aes(x=GRS),
binwidth = 0.2,
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_histogram(data = filter(mydata,Sex=="Male"),
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[6],0.35),#填充颜色
color="white")+
geom_histogram(data=filter(mydata,Sex=="Female"& fustat==1),
bins = 50,
aes(x=GRS),
binwidth = 0.2,
fill=isfahan[2],
color="white")+
geom_histogram(data=filter(mydata,Sex=="Male"& fustat==1),
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,
fill=isfahan[6],
color="white")+
coord_cartesian(xlim = c(-3,3.5))
4.设置文字标签
ggplot()+
geom_histogram(data=filter(mydata,Sex=="Female"),
bins = 50,
aes(x=GRS),
binwidth = 0.2,
fill=colorspace::lighten(isfahan[2],0.35),#填充颜色
color="white")+
geom_histogram(data = filter(mydata,Sex=="Male"),
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,#柱子宽度,相当于hist中的breaks=
fill=colorspace::lighten(isfahan[6],0.35),#填充颜色
color="white")+
geom_histogram(data=filter(mydata,Sex=="Female"& fustat==1),
bins = 50,
aes(x=GRS),
binwidth = 0.2,
fill=isfahan[2],
color="white")+
geom_histogram(data=filter(mydata,Sex=="Male"& fustat==1),
bins = 50,
aes(x=GRS,y=-..count..),
binwidth = 0.2,
fill=isfahan[6],
color="white")+
annotate(geom="label",x=3.5,y=5000,
label="Female with AF",
fill=isfahan[2],
color="white",
size=6,
hjust=1)+
annotate(geom="label",x=3.5,y=7000,
label="Female without AF",
fill=colorspace::lighten(isfahan[2],0.35),
color="white",
size=6,
hjust=1)+
annotate(geom="label",x=3.5,y=-5000,
label="Male with AF",
fill=isfahan[6],
size=6,
color="white",
hjust=1)+
annotate(geom="label",x=3.5,y=-7000,
label="Male without AF",
fill=colorspace::lighten(isfahan[6],0.35),
color="white",
size=6,
hjust=1)+
geom_hline(yintercept =0,color="white",linewidth=0.5)+
coord_cartesian(xlim = c(-3,3.5))+
labs(x="polygenic risk score (PRS)",y="count")+#设置x,y轴标签
theme_minimal()+#设置主题类型
theme(panel.grid.minor = element_blank(),
plot.background = element_rect(fill="white",color = NA), #设置背景颜色
plot.title = element_text(face = "bold"),
axis.title = element_text(face="bold",,size=15), #设置横纵坐标标签的大小,字体
strip.text = element_text(face = "bold",size=rel(0.8),hjust = 0), #设置背景线条文字的字体
strip.background = element_rect(fill="grey80",color = NA), #设置背景线条的颜色
legend.title = element_text(face="bold"))