[R] Error Bars ggplot2

Adam Gabbert adamjgabbert at gmail.com
Thu Jul 26 19:49:37 CEST 2012


Hi Dennis,

Part of my problem could be that I'm unsure how to nest another variable
withn spd.f.  Perhaps if I give a better explanation of my goal things will
make more sense.

My intent is to calculate two sets of confidence intervals to show the
benefits of a DOE approach versus a Non-DOE approach.  For example, I want
to calculate the confidence intervals for "f" and "s" in two ways.  First
using a DOE approach, by taking the mean of the "f" or "s" values
plus/minus t-value at a 95%ci with 10df multiplied by the standard error
[mean(f-values) +- tval(.95ci,10df)*std.err].  Second using a "bin"
specific approach which only looks at the 6 "f" or "s" values (i.e,
mean(f-values) +- tval(.95ci, 5df)*std.err.mean).

This will leave me with two confidence intervals, that I want to plot side
by side to show that the DOE approach confidence interval will be smaller
for most cases.  I have attached a sample plot that shows the plot layout
I'm trying to get.

Thanks

AG



Hi:

Your code makes no sense because the variable by which you want to
dodge is the same as the one you're using on the x-axis. Dodging by
subgroups is an application of visualizing nested data, which you
don't have (at least in the state that you posted). For your data,
this would work:

ggplot(data, aes(x = spd, y = r, colour = spd)) +
   geom_errorbar(aes(ymin = 3, ymax = 5), width = 0.1) +
   geom_point()

I don't understand the point of the second geom_errorbar() call, so
I'm just avoiding it.

In order to dodge (appose groups in factor B side by side within each
level of factor A), you need a third variable whose values are nested
within levels of spd.f.

HTH,
Dennis

On Thu, Jul 26, 2012 at 6:03 AM, Adam Gabbert <adamjgabbert at gmail.com>
wrote:
> Hello,
>
> I'm attempting to plot error bars side by side rather than stacked on top
> of each other with ggplot2.  Here is the sample code I am using:
>
> #Code
>
> #Data
> spd<-c("s","f","f","s","f","s","s","s","f","f","s","f")
> r<-c(4.9,3.2,2.1,.2,3.8,6.4,7.5,1.7,3.4,4.1,2.2,5)
>
> #Turn spd into a factor
> spd.f<-factor(spd)
>
> #Place data into a data frame
> data<-data.frame(cbind(spd.f,r))
>
> #Load ggplot2
> library(ggplot2)
>
> #Create plot
> pd<-position_dodge(.2)
> myplot<-ggplot(data,aes(x=spd,y=r,colour=spd))+
>   geom_errorbar(aes(ymin=3,ymax=5),width=.1)+
>   geom_point()+
>   geom_errorbar(aes(ymin=1,ymax=6),width=.1,colour="black",position=pd)
> #Display plot
> myplot
>
> I have attached a plot that my sample code produces.  As you can see the
> error bars are stacked.  How can I get them to plot side by side?
>
> Thanks
>
> AG


More information about the R-help mailing list