R:如何以图形方式绘制经调整的均值,SE,CI ANCOVA

时间:2014-05-06 13:33:51

标签: r types plot mean

我遇到了如何以图形方式绘制ANCOVA结果的问题,如果有人能帮助我,我会非常感激。

我有两个由23个基线值(协变量)组成的载体和治疗后的23个值(自变量),我有两个因子同时具有两个水平。我创建了一个ANCOVA模型并计算了调整后的平均值,标准误差和置信区间。例如:

library(effects)

baseline = c(0.7672,1.846,0.6487,0.4517,0.5599,0.2255,0.5946,1.435,0.5374,0.4901,1.258,0.5445,1.078,1.142,0.5,1.044,0.7824,1.059,0.6802,0.8003,0.5547,1.003,0.9213)
after_treatment = c(0.4222,1.442,0.8436,0.5544,0.8818,0.08789,0.6291,1.23,0.4093,0.7828,-0.04061,0.8686,0.8525,0.8036,0.3758,0.8531,0.2897,0.8127,1.213,0.05276,0.7364,1.001,0.8974)

age = factor(c(rep(c("Young","Old"),11),"Young")) 
treatment = factor(c(rep("Drug",12),rep("Placebo",11)))

ANC = aov(after_treatment ~ baseline + treatment*age)

effect_treatage = effect("treatment*age",ANC)
data.frame(effect_treatage)

  treatment   age       fit        se     lower     upper
1      Drug   Old 0.8232137 0.1455190 0.5174897 1.1289377
2   Placebo   Old 0.6168641 0.1643178 0.2716452 0.9620831
3      Drug Young 0.5689036 0.1469175 0.2602413 0.8775659
4   Placebo Young 0.7603360 0.1462715 0.4530309 1.0676410

现在我想使用R.

创建这些效果值的图形框图

使用列在Excel中手动绘制值会产生以下结果:

Example

是否有人知道如何用R绘制这些结果,包括标准误差和置信区间?

非常感谢!

1 个答案:

答案 0 :(得分:1)

准备输出data.frame以进行绘图:

数据准备:

只保留合并后的列age_treatment作为索引,我使用了逗号分隔符,您可以在sep=" "中更改

effect_df<-data.frame(effect_treatage)
effect_df[,1]<-paste(effect_df[,2],effect_df[,1],sep=",")
effect_df<-effect_df[,-2]
names(effect_df)[1]<-'age_treatment'
> effect_df
  age_treatment       fit        se     lower     upper
1      Old,Drug 0.8232137 0.1455190 0.5174897 1.1289377
2   Old,Placebo 0.6168641 0.1643178 0.2716452 0.9620831
3    Young,Drug 0.5689036 0.1469175 0.2602413 0.8775659
4 Young,Placebo 0.7603360 0.1462715 0.4530309 1.0676410

<强>剧情

使用ggplot2已更新

ggplot(effect_df, aes(x=age_treatment, y=fit)) + 
geom_bar(stat="identity",fill="#619CFF")  + 
geom_errorbar(aes(ymin=fit-se, ymax=fit+se,), width=.1,col="black") +
geom_point(aes(x=age_treatment,y=lower),size=5,shape=21)+
geom_point(aes(x=age_treatment,y=upper),size=5,shape=21)

<强>输出

enter image description here