R 中定制的事件时间图

问题描述 投票:0回答:1

我正在尝试以这种方式设计事件时间图:

我一直在工作的数据和代码如下:

structure(list(type = c("dynamic", "dynamic", "dynamic", "dynamic", 
"dynamic", "dynamic", "dynamic", "dynamic", "dynamic"), term = c("ATT(-4)", 
"ATT(-3)", "ATT(-2)", "ATT(-1)", "ATT(0)", "ATT(1)", "ATT(2)", 
"ATT(3)", "ATT(4)"), event.time = c(-4, -3, -2, -1, 0, 1, 2, 
3, 4), estimate = c(0.26515256993999, 0.151872124263966, 0.0869620743535941, 
0.511898495150082, 1.70892342183257, 1.47487464226574, 1.27266126343054, 
1.41148383616974, 2.09672811604038), std.error = c(0.165251036032427, 
0.125780865480357, 0.139673647128451, 0.0843033105456399, 0.132559563887908, 
0.153858075990952, 0.165464288658923, 0.226006579280847, 0.291119261169366
), conf.low = c(-0.0587335090914985, -0.09465384202181, -0.186793243607526, 
0.346667042703132, 1.44911145080594, 1.17331835459285, 0.948357216931508, 
0.968519080510181, 1.52614484894251), conf.high = c(0.589038648971479, 
0.398398090549743, 0.360717392314714, 0.677129947597032, 1.96873539285921, 
1.77643092993863, 1.59696530992956, 1.8544485918293, 2.66731138313825
), point.conf.low = c(-0.0587335090914985, -0.09465384202181, 
-0.186793243607526, 0.346667042703132, 1.44911145080594, 1.17331835459285, 
0.948357216931508, 0.968519080510181, 1.52614484894251), point.conf.high = 
 c(0.589038648971479, 
0.398398090549743, 0.360717392314714, 0.677129947597032, 1.96873539285921, 
1.77643092993863, 1.59696530992956, 1.8544485918293, 2.66731138313825
), prepost = c("pre", "pre", "pre", "pre", "post", "post", "post", 
"post", "post")), class = "data.frame", row.names = c(NA, -9L
))



grafica<-B|>ggplot(aes(x=event.time,y=estimate))+
  geom_point(position=position_dodge(w=0.25))+
  geom_smooth(method="loess",level=0.95,linetype=2,size=0.5,span=0.8,alpha=0.1)+
  geom_errorbar(aes(ymin=conf.low,ymax=conf.high,width=0.1,color=prepost),position=position_dodge(w=0.25))+
  scale_color_manual("Time",values=colores[c(4,1)])+theme_classic()+
  
  
  geom_hline(yintercept = 0,linetype = "longdash" )+geom_vline(xintercept = 0 )+
  scale_x_discrete(name ="event time", limits=c(-4,-3,-2,-1,0,1,2,3,4))+
  # scale_color_manual("Gender",values = colores[c(4,1)])+
  labs(title= "Plot")+
  ylab("ylab")+
  xlab("Time to event")

grafica

但这并不正是我正在寻找的。我想连接这些点并创建条带,以便看到与提供的图表类似的内容。

r ggplot2 plot confidence-interval line-plot
1个回答
0
投票

我们可以使用

geom_smooth()
geom_ribbon()
 而不是使用 
geom_errorbar()

library(ggplot2)

grafica <- ggplot(df, aes(x=event.time, y=estimate, group=1)) +
  geom_line() +
  geom_point(aes(color=prepost)) + 
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), fill="grey80", alpha=0.5) +
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color=prepost), width=0.1, 
                position=position_dodge(width=0.25)) +
  scale_color_manual(values=c("pre"="steelblue3", "post"="firebrick3")) +
  theme_classic() +
  geom_hline(yintercept=0, linetype="longdash") +
  geom_vline(xintercept=0) +
  scale_x_continuous(name="Event Time", breaks=c(-4, -3, -2, -1, 0, 1, 2, 3, 4)) +
  labs(title="My plot", y="labely", x="Time to Event")

© www.soinside.com 2019 - 2024. All rights reserved.