[R] Error Bars ggplot2

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Jul 27 10:37:28 CEST 2012


How about this?

Andpleasemakeusofthelargekeyatthebottomofyourkeyboard.Itmakescodemuchmorereadable.

library(ggplot2)
spd <- factor(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)
dataset <- data.frame(spd, r)
dataset <- rbind(cbind(dataset, Type = "DOE", delta = 2),
cbind(dataset, Type = "Non-DOE", delta = 3))
ggplot(dataset, aes(x = spd:factor(r), y = r, ymin = r - delta, ymax = r + delta, colour = spd, linetype = Type)) + geom_errorbar() + geom_point()


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Adam Gabbert
Verzonden: donderdag 26 juli 2012 19:50
Aan: r-help op r-project.org
Onderwerp: Re: [R] Error Bars ggplot2

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 op 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
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list