繁体   English   中英

关于点估计和置信区间的ggplot

[英]ggplot on point estimates and confidence intervals

我正在尝试使用ggplot绘制数据帧,它看起来像http://www.ats.ucla.edu/stat/r/dae/logit.htm底部的图。

       a<-data.frame(Year=c("2012","2012","2012","2013","2013","2013","2014","2014","2014"),
          Engagement=rep(c("low","med","high"),3),
         cost=c(4464.88,4690.14,4342.72,5326.63,5000.03,3967.02,4646.27,4282.38,3607.79),
         lower=c(4151.4,5027.51,4095.73,4366.82,4682.85,3715.86,3775.25,3642.41,3235.43),
         upper=c(4778.35,5625.75,5196.81,5013.45,5317.2,4848.89,4910.19,4291.64,3980.14))

我试过了:

k<-ggplot(a,aes(x=Year,y=cost))
k+geom_ribbon(aes(ymin=lower,ymax=upper,fill=Engagement),alpha=0.2)+
geom_pointrange(aes(x=Year,y=cost,ymin=lower,ymax=lower),size=1,width=0.2,color="blue")

我感谢所有帮助。

我也尝试过:

pd <- position_dodge(0.1)
k<-ggplot(a,aes(x=Year,y=cost))
k+geom_ribbon(aes(ymin=lower,ymax=upper,fill=Engagement),alpha=0.2)+
  geom_line(position=pd,aes(color=Engagement))

错误信息:

ymax not defined: adjusting position using y instead
geom_path: Each group consist of only one observation. 
Do you need to adjust the group aesthetic?

谢谢大家,问题解决了:

k+geom_line(aes(group=Engagement,color=Engagement))+
  geom_errorbar(aes(ymin=lower,ymax=upper,color=Engagement,width=0.2))

在此处输入图片说明

我假设“看起来”是指将色带添加到图形中。 如果是这样,则问题是从词干Year在变量a data.frame。 它当前具有因子类,并且需要为数字。

如果在调用ggplot图之前添加此代码,则应该看到功能区:

a$Year <- as.numeric(a$Year)

你也可以修改您的整个分配a以下几点:

a<-data.frame(Year=as.numeric(c("2012","2012","2012","2013","2013","2013","2014","2014","2014")),
              Engagement=rep(c("low","med","high"),3),
              cost=c(4464.88,4690.14,4342.72,5326.63,5000.03,3967.02,4646.27,4282.38,3607.79),
              lower=c(4151.4,5027.51,4095.73,4366.82,4682.85,3715.86,3775.25,3642.41,3235.43),
              upper=c(4778.35,5625.75,5196.81,5013.45,5317.2,4848.89,4910.19,4291.64,3980.14))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM