[R] Confidence bands in ggplot2

Abhijit Dasgupta adasgupta at araastat.com
Fri Jul 8 06:00:06 CEST 2011


It's basically a question of layering, and the order in which the layers are drawn. Draw the pointranges first and then the points:

qplot(x=as.factor(sch), y=est, ymin=lower.95ci, ymax=upper.95ci, geom='pointrange')+
	geom_point(aes(x=as.factor(sch), y=est), color='red')+...

On Jul 7, 2011, at 6:16 PM, Christopher Desjardins wrote:

> Thanks that worked perfectly. One thing if I may. Is it possible to make the center dot red and the lines connecting the dots black?
> 
> Thanks,
> Chris
> 
> 
> On Jul 7, 2011, at 5:10 PM, Abhijit Dasgupta, PhD wrote:
> 
>> You can easily do this by:
>> 
>> qplot(x=as.factor(sch),y=est, geom='point', colour='red') +
>> geom_pointrange(aes(x=as.factor(sch), y=est, ymin=lower.95ci, ymax=upper.95ci))+
>> xlab('School') + ylab("Value-added")+theme_bw()
>> 
>> 
>> 
>> 
>> On 07/07/2011 05:55 PM, Christopher Desjardins wrote:
>>> Hi,
>>> I have the following data:
>>> 
>>>> est
>>>     sch190      sch107      sch290      sch256      sch287      sch130      sch139
>>> 4.16656026  2.64306071  4.22579866  6.12024789  4.49624748 11.12799127  1.17353917
>>>     sch140      sch282      sch161      sch193      sch156      sch288      sch352
>>> 3.48197696 -0.29659410 -1.99194986 10.23489859  7.77342138  6.77624539  9.66795001
>>>     sch368      sch225      sch301      sch105      sch353      sch291      sch179
>>> 7.20229569  4.41989204  5.61586860  5.99460203 -2.65019242 -9.42614560 -0.25874193
>>>     sch134      sch135      sch324      sch360         bb1
>>> 3.26432479 10.52555091 -0.09637968  2.49668858 -3.24173545
>>> 
>>>> se
>>>   sch190    sch107    sch290    sch256    sch287    sch130    sch139    sch140
>>> 3.165127  3.710750  4.680911  6.335386  3.896302  4.907679  4.426284  4.266303
>>>   sch282    sch161    sch193    sch156    sch288    sch352    sch368    sch225
>>> 3.303747  4.550193  3.995261  5.787374  5.017278  7.820763  7.253183  4.483988
>>>   sch301    sch105    sch353    sch291    sch179    sch134    sch135    sch324
>>> 4.076570  7.564359 10.456522  5.705474  4.247927  5.671536 10.567093  4.138356
>>>   sch360       bb1
>>> 4.943779  1.935142
>>> 
>>>> sch
>>> [1] "190" "107" "290" "256" "287" "130" "139" "140" "282" "161" "193" "156" "288"
>>> [14] "352" "368" "225" "301" "105" "353" "291" "179" "134" "135" "324" "360" "BB"
>>> 
>>> 
>>> From this data I have created 95% confidence intervals assuming a normal distribution.
>>> 
>>> lower.95ci<- est - se*qnorm(.975)
>>> upper.95ci<- est + se*qnorm(.975)
>>> 
>>> What I'd like to do is plot the estimate (est) and have lines attach to the points located in lower.95ci and upper.95ci.  Presently I am doing the following:
>>> 
>>> qplot(x=as.factor(sch),y=lower.95ci) + geom_point(aes(x=as.factor(sch),y=upper.95ci),colour="black") + geom_point(aes(x=as.factor(sch), y=est),colour="red") + ylab("Value-Added") + xlab("School") + theme_bw()
>>> 
>>> Which creates this graph --->   http://dl.dropbox.com/u/1501309/value_added_test.pdf
>>> 
>>> That's fine except that it doesn't connect the points vertically. Does anyone know how I could make the 'black' points connect to the 'red' point, i.e. show confidence bands?
>>> 
>>> Thanks,
>>> Chris
>>> 
>>> 
>>> 	[[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list