R Barplot Error Bars Arrows
Contents |
Diet & Nutrition (28) Education (1) Evolution (35) Human Ecology (75) Infectious Disease (66) LaTeX (5) Primates (9) R (12) science (17) Social Network Analysis (17) Statistics (16) Teaching (10) Uncategorized (28) adding standard error bars in r Meta Log in Entries RSS Comments RSS WordPress.org ← Latest Swine Flu Epidemic barplot with error bars ggplot2 Curve for the United States Stanford Workshop in Biodemography → Plotting Error Bars in R August 24th, 2009 · error bar in r 52 Comments · R One common frustration that I have heard expressed about R is that there is no automatic way to plot error bars (whiskers really) on bar plots. I just
Error.bar Function R
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) != 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 scatter plot with error bars in r 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 reporting the results of scientific experiments: adjacent bars representing the treatment and the control with 95% confidence intervals on the estimates of the means. The trick here is to create a 2 x n matrix of your bar values, where each row holds the values to be compared (e.g., treatment vs.
[ subject ] [ author ] Hi Katherine, If you have the original data, you can use the function barplot.CI from the package sciplot errbar r which will calculate the means and CI's for you. Examples are at: http://mutualism.williams.edu/sciplot
Barplot With Error Bars Matlab
If you have the means and CI's already calculated, the following function will do what you want: CI.plot <- function(mean,
Summaryse R
std, ylim=c(0, max(CI.H)), ...) { CI.H <- mean+1.96*std # Calculate upper CI CI.L <- mean-1.96*std # Calculate lower CI xvals <- barplot(mean, ylim=ylim, ...) # Plot bars arrows(xvals, mean, xvals, CI.H, angle=90) http://monkeysuncle.stanford.edu/?p=485 # Draw error bars arrows(xvals, mean, xvals, CI.L, angle=90) } On Mon, 2007-10-22 at 17:03 -0400, Katherine Jones wrote: > Apologies if this has been asked before. I am having trouble > understanding the R mailing list never mind R! > > I am relatively new to R having migrated from Minitab nd SPSS. I > have managed to do some more complicated statistics such as https://stat.ethz.ch/pipermail/r-help/2007-October/143937.html > hierarchical partitioning of variance on an 80,000 record dataset but > have to admit that drawing a simple bar plot I could do by hand is > proving extremely difficult! > > I have Crawley's 'The R book' which is proving a big help, but still > does not provide me with all of the information I need. > > What I want to do:- > I have a small dataset, with 'rate' as the dependent variable (y axis > variable) and two groups ('cat' and 'box') which I have calculated > the mean and standard error for and would like to display as a bar > plot. I would like to display the standard error for each group NOT a > pooled standard error (i.e. error bars will be different heights on > the two bars). In the Crawley book, he just repeats the same error > bars across all groups, and I don't know how to specify the different > error for each bar. > > What I have managed to do so far:- > -calculate mean and standard error > -plots means with confidence intervals > -plot means as ba
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 http://stackoverflow.com/questions/15063287/add-error-bars-to-show-standard-deviation-on-a-plot-in-r Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://strata.uga.edu/6370/rtips/barPlotErrorbars.html or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Add error bars to show standard deviation error bar 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 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 with error bars plot statistics standard-deviation share|improve this question edited Oct 16 '14 at 3:43 Craig Finch 11417 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| 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,20984683 add a comment| up vote 19 down vote A solution with ggplot2 : qplot(x,y)+geom_erro
a bar plot is one of those things. The segments() command lets you draw line segments, provided you specify the coordinates of the beginning and end of the segments. Since an error bar is just a line segment, the x coordinates for the start and the end are the center of the top of the bar, and the y coordinates are the top of the bar plus or minus the length of the error bar. I assumed that the centers of the bars would be integers, that is, if you had three bars, that their centers would be at 1, 2, and 3. That is not true - the centers of the bars are not integers, which makes finding their locations trickier. You could use the locator() function to find the centers of the bars, but clicking on points can be imprecise. It is also impractical when there are many bars. The easy solution to finding the bar centers is in the barplot() command itself: in addition to plotting the graph, the function also returns a vector of the centers of each bar. Here is how it all works. First, I will use the mean lengths of three small mammals, with the standard errors of those lengths. names <- c("squirrel", "rabbit", "chipmunk") means <- c(23, 28, 19) standardErrors <- c(1.2, 1.7, 0.9) Because the top of the plot is scaled to the tallest bar, the error bars will get clipped if I add them. To prevent this, I calculate the top of the highest bar; here, the error bars I am using are plus or minus two standard errors. plotTop <- max(means+standardErrors*2) First, I will plot the graph, with the bars filled with gray, with y-axis labels rotated (las=1), and with the limits on the y-axis expanded so they will include the error bars. I assign the barplot() command to barCenters, because the command returns a vector of the centers of the bars. barCenters <- barplot(means, names.arg=names, col="gray", las=1, ylim=c(0,plotTop)) Finally, I add the error bars using segments(), where the x coordinates for the beginning and end of each bar are saved in barCenters. The y coordinate of the bottom of each bar is the mean minus two standard errors, and the y coordinate of the top of each bar is the mean plus two standard errors. To make the bars bolder, I double the line width (lwd=2). segments(barCenters, means-standardErrors*2, barCenters, means+standardErrors*2, lwd=2) Alternatively, if you prefer your error bars to have ticks at the end, use the arrows() command, setting code=3 to put ticks at both ends, and setting angle=90 to place the ticks at right angles to the main error bar. arrows(barCenters, means-standardErrors*2, barCenters, means+standardErrors*2, lwd=2, angle=90, c