绘制高级频率分布直方图——ggplot2

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"))

好的,我来回答您的问题。首先,需要明确一下白噪声的SEIR传染病模型是什么。这是一种用于描述传染病在人群中传播的数学模型,其中考虑了人口流动和感染率等因素。在模型中,人群被分为若干个亚群,每个亚群中的个体被分为易感者、潜伏者、感染者和康复者四类。该模型中的白噪声项用于描述随机性,即在相同条件下,不同时间的传染病爆发情况可能会有所不同。 针对您的问题,我提供一些示例代码,帮助您在Matlab中绘制白噪声的SEIR传染病模型中S的频率直方图和边际密度函数图。假设您已经生成了一组包含S的数据,可以使用以下代码绘制频率直方图和边际密度函数图: ```matlab % 生成一组包含S的数据,假设为s_data s_data = randn(1000,1); % 绘制频率直方图 figure; histogram(s_data,'Normalization','probability'); title('Frequency distribution histogram of S'); % 绘制边际密度函数图 figure; ksdensity(s_data); title('Marginal density function of S'); ``` 在这个例子中,我们生成了一个包含1000个随机数的数据集,模拟了S的分布情况。使用Matlab中的“histogram”函数和“ksdensity”函数分别绘制了S的频率直方图和边际密度函数图。其中,“Normalization”参数用于将频率转换为概率。 需要注意的是,生成的数据应该符合白噪声的SEIR传染病模型的假设,即应该包含随机性。如果您没有具体的数据,可以使用模型中的参数和初始值,结合数值方法(如欧拉法)模拟生成一组数据,再进行绘图分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值