如何使用stat_summary()但不同的geom属性覆盖两个ggplots

时间:2019-04-02 16:21:55

标签: r ggplot2

我正在绘制具有95置信区间的折线图,但是,我希望一个条件的95CI范围可以显示为功能区而不是误差线,以显示上下边界的范围。

下面的代码显示了95CI的折线图。

df1<-structure(list(subject = c("S11", "S11", "S11", "S11", "S11", 
"S12", "S12", "S12", "S12", "S12", "S12"), condition = c("placebo", 
"placebo", "placebo", "placebo", "placebo", "Drug1", "Drug1", 
"Drug1", "Drug1", "Drug1", "Drug1"), Day = c(1, 2, 3, 4, 5, 1, 
2, 3, 4, 5, 6), Effect = c(-0.053, 0.02831, 
0.056, 0.450, 0.29, 0.011, 
-0.039, -0.01293, 0.042, 
-0.01568, 0.080)), row.names = c(NA, 
-11L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(df1,
             aes(x=Day,y=Effect,color= condition ,fill= condition)) +
  stat_summary(fun.y="mean", geom="line",size=1.5,alpha=1)+
  stat_summary(fun.y ='mean',geom ="point", size = 1,alpha=0.8,show.legend = FALSE)+
  #geom_ribbon(aes(ymin=min.Uv, ymax=max.Uv), alpha=0.7,fill='yellow', colour=NA)+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", fun.args = list(mult = 1),size=1)


上面已注释的行(geom_ribbon(aes(ymin=min.Uv, ymax=max.Uv))用作另一种情况的95CI的可视化。 我想知道是否有更有效的方法,因为fun.data = mean_cl_normal的功能在做同样的事情。 因此,我用另一个具有相似绘图语法但在fig1中不同的图形来实现gemo='smooth'。主要问题是,是否可以通过添加另一个geom_ribbon(aes(ymin=min.Uv, ymax=max.Uv)来显示95CI范围来替换(gemo='smooth')?

df2<-structure(list(subject = c("S02", "S02", "S02", "S02", "S02"), 
    condition = c("Drug2", "Drug2", "Drug2", "Drug2", "Drug2"
    ), Day = c(1, 2, 3, 4, 5), Effect = c(1.6592, 1.8384, 
    1.809, 1.807, 1.651)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

fig1+stat_summary(data=df2,aes(x=Day,y=Effect),
                  fun.data ="mean_cl_normal", geom = "smooth", , fun.args = list(mult = 1))

我得到了重叠的折线图,但功能区/平滑度缺失。任何评论都非常赞赏!

0 个答案:

没有答案