Draw Error Bar In R
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
How To Draw Error Bar In Excel
Learn more about Stack Overflow the company Business Learn more about hiring developers or how to draw an error bar by hand posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow how to draw error bar in origin Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Scatter plot with error bars up vote 21
Error Bar Plot In R
down vote favorite 11 How can I generate the following plot in R? Points, shown in the plot are the averages, and their ranges correspond to minimal and maximal values. I have data in two files (below is an example). x y 1 0.8773 1 0.8722 1 0.8816 1 0.8834 1 0.8759 1 0.8890 1 0.8727 2 0.9047 2 0.9062 2 0.8998 2 0.9044 2 0.8960 .. ...
Scatter Plot With Error Bars In R
r plot share|improve this question edited Oct 23 '12 at 15:10 Roland 73.2k463102 asked Oct 23 '12 at 14:29 sherlock85 1521313 Since you clearly don't want a boxplot, I changed the title of your question in order to reflect what you really want. –Roland Oct 23 '12 at 15:11 1 also plotrix::plotCI, gplots::plotCI, library("sos"); findFn("{error bar}") –Ben Bolker Oct 23 '12 at 17:29 add a comment| 5 Answers 5 active oldest votes up vote 51 down vote accepted First of all: it is very unfortunate and surprising that R cannot draw error bars "out of the box". Here is my favourite workaround, the advantage is that you do not need any extra packages. The trick is to draw arrows (!) but with little horizontal bars instead of arrowheads (!!!). This not-so-straightforward idea comes from the R Wiki Tips and is reproduced here as a worked-out example. Let's assume you have a vector of "average values" avg and another vector of "standard deviations" sdev, they are of the same length n. Let's make the abscissa just the number of these "measurements", so x <- 1:n. Using these, here come the plotting commands: plot(x, avg, ylim=range(c(avg-sdev, avg+sdev)), pch=19, xlab=
|| is.character(x)) "" else as.character(substitute(y)), add=FALSE, lty=1, type='p', ylim=NULL, lwd=1, pch=16, Type=rep(1, length(y)), ...) Arguments x vector of numeric x-axis values (for vertical error error bars in r barplot bars) or a factor or character variable (for horizontal error bars, x
Error.bar Function R
representing the group labels) y vector of y-axis values. yplus vector of y-axis values: the tops of errbar r the error bars. yminus vector of y-axis values: the bottoms of the error bars. cap the width of the little lines at the tops and bottoms of the error http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars bars in units of the width of the plot. Defaults to 0.015. main a main title for the plot, see also title. sub a sub title for the plot. xlab optional x-axis labels if add=FALSE. ylab optional y-axis labels if add=FALSE. Defaults to blank for horizontal charts. add set to TRUE to add bars to an existing http://svitsrv25.epfl.ch/R-doc/library/Hmisc/html/errbar.html plot (available only for vertical error bars) lty type of line for error bars type type of point. Use type="b" to connect dots. ylim y-axis limits. Default is to use range of y, yminus, and yplus. For horizonal charts, ylim is really the x-axis range, excluding differences. lwd line width for line segments (not main line) pch character to use as the point. Type used for horizontal bars only. Is an integer vector with values 1 if corresponding values represent simple estimates, 2 if they represent differences. ... other parameters passed to all graphics functions. Details errbar adds vertical error bars to an existing plot or makes a new plot with error bars. It can also make a horizontal error bar plot that shows error bars for group differences as well as bars for groups. For the latter type of plot, the lower x-axis scale corresponds to group estimates and the upper scale corresponds to differences. The spacings of the two scales are identical but the scale for differences has its origin
error bars Two within-subjects variables Note about normed means Helper functions Problem You want to http://cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ plot means and error bars for a dataset. Solution To make graphs with ggplot2, the data must be in a data frame, and in https://stat.ethz.ch/pipermail/r-help/2000-November/009029.html “long” (as opposed to wide) format. If your data needs to be restructured, see this page for more information. Sample data The examples below error bar 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 draw error bar 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 <
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 these calls don't do is (1) to size the plot appropriately so the upper and lower 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 > >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 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