R Scatter Plot With X And Y Error Bars
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more scatter plot with error bars r about Stack Overflow the company Business Learn more about hiring developers or posting ads error.bar function r with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Error Bars In R
Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ggplot2 : Adding two errorbars to each point in scatterplot up vote
Errbar R
13 down vote favorite 6 I need to plot two error-bars on each point in a scatterplot. The usual is vertical error-bars that corresponds to the error on the points y-value, but I need to add the error-bar associated with the X-axis (horizontal) as well. I could probably do this with some abline command, but thought there might be a more clever way to do it with ggplot2? r summaryse in r ggplot2 share|improve this question asked Feb 10 '12 at 17:03 Jens Nielsen 9515 1 I believe there's a geom_errorbarh that takes x, xmin and xmax analogously to geom_errorbar. –joran Feb 10 '12 at 17:08 permalink.gmane.org/gmane.comp.lang.r.ggplot2/3231 –Ben Bolker Feb 10 '12 at 17:09 add a comment| 1 Answer 1 active oldest votes up vote 18 down vote accepted Just for completion's sake, following up on my comment, here is a simply (albeit ugly) example: df <- data.frame(x = 1:10, y = 1:10, ymin = (1:10) - runif(10), ymax = (1:10) + runif(10), xmin = (1:10) - runif(10), xmax = (1:10) + runif(10)) ggplot(data = df,aes(x = x,y = y)) + geom_point() + geom_errorbar(aes(ymin = ymin,ymax = ymax)) + geom_errorbarh(aes(xmin = xmin,xmax = xmax)) share|improve this answer answered Feb 10 '12 at 17:16 joran 102k11218272 2 Thanks a lot for that reply! it took me some time to reproduce your results with my own data as in my data the columns are NOT named "x" and "y", which (apparently) means that for the geom_errorbar you need to pass the x coordinate, that is: geom_errorbar(aes(x=var, ymin=...)) and for the geom_errorbarh both x and y, so: geom_errorbarh(aes(x=var1, y=var2, xmin=...)). This last detail of the horizontal
deparse(substitute(x)), ylab= deparse(substitute(y)), ...) r arrows Arguments x vector of x values. y vector of ggplot2, horizontal error bars y values. yplus vector of y values: the tops of the error bars. yminus vector http://stackoverflow.com/questions/9231702/ggplot2-adding-two-errorbars-to-each-point-in-scatterplot of y values: the bottoms of the error bars. cap the width of the little lines at the tops and bottoms of the error bars in units of the width http://artax.karlin.mff.cuni.cz/r-help/library/sfsmisc/html/errbar.html of the plot. Default is 0.015. ylim (numeric of length 2): the y-axis extents with a sensible default. xlab, ylab axis labels for the plot, as in plot.default. ... Graphical parameters (see par) may also be supplied as arguments to this function. Author(s) Originally Charles Geyer, U.Chicago, early 1991; then Martin Mächler. See Also errbar in package Hmisc is similar. Examples y <- rnorm(10); d <- 1 + .1*rnorm(10) errbar(1:10, y, y + d, y - d, main="Error Bars example") [Package sfsmisc version 1.0-20 Index]
That's certainly a simpler solution. It might be worth wrapping a few arrows() calls up in some kind of simple errorbar function (just so it's slightly more accessible to newcomers). The only two things my function did that https://stat.ethz.ch/pipermail/r-help/2000-November/009029.html these calls don't do is (1) to size the plot appropriately so the upper and lower http://cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ limits of the errors are within the plot, (2) to draw the segments/arrows first so that one can add points with pch=19 and bg=par("bg") to get open points without lines going through them. On Thu, 9 Nov 2000, Emmanuel Paradis wrote: > At 14:07 08/11/00 -0500, Ben Bolker wrote: > > > > I'm going to take the liberty of reposting this function, which is based > error bars >on one that Bill Venables posted a while back. I've tweaked with it a bit > >to add functionality. It will do horizontal bars or vertical bars, but > >not (yet) both simultaneously (the hardest thing about that is deciding on > >what format you want the data supplied in). > > > > There's also a help file supplied below. > > > > Should this (after appropriate tweaking/polishing/testing/revision) go > >into the main R code base? It seems like a pretty basic scatter plot with function to me > >... > > [...] > > >On Wed, 8 Nov 2000, Mike Beddo wrote: > > > >> I'm a newcomer to R. I can't seem to find any documentation how to add > >> error bars to points in scatter plots. I guess I could plot the points, > >> then compute and plot line segments in the X and/or Y directions to > >> represent the errors? > >> > >> - Mike > > I think using arrows(..., code=3, angle=90, ...) is quite simple, e.g.: > > x <- rnorm(10) > y <- rnorm(10) > se.x <- runif(10) > se.y <- runif(10) > plot(x, y, pch=22) > arrows(x, y-se.y, x, y+se.y, code=3, angle=90, length=0.1) > arrows(x-se.x, y, x+se.x, y, code=3, angle=90, length=0.1) > > The first arrows() draws the error bars for y, and the second one for x, > 'code=3' draws a head at both ends of the arrow, 'angle=' is the angle of > the head with the main axis of the arrow, and 'length=' is the length of > the head. You can also add usual graphic parameters (col, lwd, ...). > > > Emmanuel Paradis > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > > -- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.
error bars Two within-subjects variables Note about normed means Helper functions Problem You want to plot means and error bars for a dataset. Solution To make graphs with ggplot2, the data must be in a data frame, and in “long” (as opposed to wide) format. If your data needs to be restructured, see this page for more information. Sample data The examples below will the ToothGrowth dataset. Note that dose is a numeric column here; in some situations it may be useful to convert it to a factor. tg <- ToothGrowth head(tg) #> len supp dose #> 1 4.2 VC 0.5 #> 2 11.5 VC 0.5 #> 3 7.3 VC 0.5 #> 4 5.8 VC 0.5 #> 5 6.4 VC 0.5 #> 6 10.0 VC 0.5 library(ggplot2) First, it is necessary to summarize the data. This can be done in a number of ways, as described on this page. In this case, we’ll use the summarySE() function defined on that page, and also at the bottom of this page. (The code for the summarySE function must be entered before it is called here). # summarySE provides the standard deviation, standard error of the mean, and a (default 95%) confidence interval tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","dose")) tgc #> supp dose N len sd se ci #> 1 OJ 0.5 10 13.23 4.459709 1.4102837 3.