R语言 如何让置信区间的线到达ggplot2中的面板边界?

mrphzbgm  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(83)

漏斗图的95%和99.7% CI如下所示:

########################
## create the dataset ##
########################

t=seq(0,1200)
lambda=0.02
sigma=sqrt(lambda/t)

alpha_95=0.05
z_both_95=qnorm(1-alpha_95/2)
lower_bound_95=lambda-z_both_95*sigma
upper_bound_95=lambda+z_both_95*sigma

alpha_997=0.003
z_both_997=qnorm(1-alpha_997/2)
lower_bound_997=lambda-z_both_997*sigma
upper_bound_997=lambda+z_both_997*sigma

df=data.frame(t,lambda,sigma,lower_bound_95,upper_bound_95,lower_bound_997,upper_bound_997)

#############################
## draw the CI on the plot ##
#############################

fp <- ggplot()+
  geom_line(aes(x = t, y = lower_bound_95), linetype = "dashed", color="orange", size=0.6, data = df) +
  geom_line(aes(x = t, y = upper_bound_95), linetype = "dashed", color="orange", size=0.6, data = df) +
  geom_line(aes(x = t, y = lower_bound_997), linetype = "dashed", color="red", size=0.6, data = df) +
  geom_line(aes(x = t, y = upper_bound_997), linetype = "dashed", color="red", size=0.6, data = df) +
  geom_hline(aes(yintercept = lambda), color="limegreen", size=0.6, data = df) +
  scale_x_continuous(limits = c(0,1200), breaks = seq(0,1200,by=150)) +
  scale_y_continuous(limits = c(-0.1,0.3),breaks = seq(-0.1,0.3,by=0.05)) +
  xlab("Total Exposure (Days)") + ylab("Exposure-Adjusted Incicence Rate") +
  theme_bw()
fp

图显示如下:

那么如何让红色和橙子虚线像绿色线一样到达面板边框呢?就像下面这个:

8yoxcaq7

8yoxcaq71#

我认为得到你想要的最简单的方法是在x轴上使用expand()

scale_x_continuous(limits = c(0,1200), breaks = seq(0,1200,by=150), expand = c(0,0)) +

相关问题