Add Error Bars Line Plot 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 Learn more about Stack Overflow the company Business Learn more about hiring ggplot2 line plot with error bars developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
R Plot Error Bars Scatter Plot
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; line graph with error bars in r it only takes a minute: Sign up Add error bars to show standard deviation on a plot in R up vote 23 down vote favorite 10 For each X-value I calculated the average Y-value and the standard deviation (sd) of error bars on line graph excel each Y-value x = 1:5 y = c(1.1, 1.5, 2.9, 3.8, 5.2) sd = c(0.1, 0.3, 0.2, 0.2, 0.4) plot (x, y) How can I use the standard deviation to add error bars to each datapoint of my plot? r plot statistics standard-deviation share|improve this question edited Oct 16 '14 at 3:43 Craig Finch 11216 asked Feb 25 '13 at 8:59 John Garreth 4572413 also see plotrix::plotCI –Ben Bolker Feb 25 '13 at 15:13 add a comment|
Line Graph With Error Bars Matlab
5 Answers 5 active oldest votes up vote 16 down vote accepted A Problem with csgillespie solution appears, when You have an logarithmic X axis. The you will have a different length of the small bars on the right an the left side (the epsilon follows the x-values). You should better use the errbar function from the Hmisc package: d = data.frame( x = c(1:5) , y = c(1.1, 1.5, 2.9, 3.8, 5.2) , sd = c(0.2, 0.3, 0.2, 0.0, 0.4) ) ##install.packages("Hmisc", dependencies=T) library("Hmisc") # add error bars (without adjusting yrange) plot(d$x, d$y, type="n") with ( data = d , expr = errbar(x, y, y+sd, y-sd, add=T, pch=1, cap=.1) ) # new plot (adjusts Yrange automatically) with ( data = d , expr = errbar(x, y, y+sd, y-sd, add=F, pch=1, cap=.015, log="x") ) share|improve this answer answered Sep 6 '13 at 14:21 R_User 3,18484581 add a comment| up vote 19 down vote A solution with ggplot2 : qplot(x,y)+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd), width=0.25) share|improve this answer answered Feb 25 '13 at 9:06 juba 23.9k55981 add a comment| up vote 18 down vote You can use segments to add the bars in base graphics. Here epsilon controls the line across the top and bottom of the line. plot (x, y, ylim=c(0, 6)) epsilon = 0.02 for(i in 1:5) { up = y[i] + sd[i] low = y[i] - sd[i] segments(x[i],low , x[i], up) segments(x[i]-epsilon, up , x[i]+epsilon, up) segments(x[i]-epsil
|| 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 bars) or a line graph with error bars stata factor or character variable (for horizontal error bars, x representing the group error.bar function r labels) y vector of y-axis values. yplus vector of y-axis values: the tops of the error bars. yminus vector
R Error Bars Ggplot2
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 bars in units of the width http://stackoverflow.com/questions/15063287/add-error-bars-to-show-standard-deviation-on-a-plot-in-r 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 plot (available only for vertical error bars) lty type of http://svitsrv25.epfl.ch/R-doc/library/Hmisc/html/errbar.html 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 shifted so that zero may be included. If at least one of the confidence intervals includes zero, a vertical do
Diet & Nutrition (28) Education (1) Evolution (35) Human Ecology (75) Infectious Disease (66) LaTeX (5) Primates (9) R (12) science (17) Social Network http://monkeysuncle.stanford.edu/?p=485 Analysis (17) Statistics (16) Teaching (10) Uncategorized (28) Meta Log in https://stat.ethz.ch/pipermail/r-help/2000-November/009029.html Entries RSS Comments RSS WordPress.org ← Latest Swine Flu Epidemic Curve for the United States Stanford Workshop in Biodemography → Plotting Error Bars in R August 24th, 2009 · 52 Comments · R One common frustration that I have heard expressed about R is error bars that there is no automatic way to plot error bars (whiskers really) on bar plots. I just encountered this issue revising a paper for submission and figured I'd share my code. The following simple function will plot reasonable error bars on a bar plot. PLAIN TEXT R: error.bar <- function(x, y, upper, lower=upper, length=0.1,...){ if(length(x) with error bars != length(y) | length(y) !=length(lower) | length(lower) != length(upper)) stop("vectors must be same length") arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...) } Now let's use it. First, I'll create 5 means drawn from a Gaussian random variable with unit mean and variance. I want to point out another mild annoyance with the way that R handles bar plots, and how to fix it. By default, barplot() suppresses the X-axis. Not sure why. If you want the axis to show up with the same line style as the Y-axis, include the argument axis.lty=1, as below. By creating an object to hold your bar plot, you capture the midpoints of the bars along the abscissa that can later be used to plot the error bars. PLAIN TEXT R: y <- rnorm(500, mean=1) y <- matrix(y,100,5) y.means <- apply(y,2,mean) y.sd <- apply(y,2,sd) barx <- barplot(y.means, names.arg=1:5,ylim=c(0,1.5), col="blue", axis.lty=1, xlab="Replicates", ylab="Value (arbitrary units)") error.bar(barx,y.means, 1.96*y.sd/10) Now let's say we want to create the very common plot in report
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 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.