R Cran Plot With 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 about Stack Overflow the company Business Learn more about hiring developers or posting error bar in r ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the scatter plot with error bars in r 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
Error Bars In R Barplot
minute: Sign up Scatter plot with error bars up vote 21 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.
Error.bar Function R
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 .. ... r plot share|improve this question edited Oct 23 '12 at 15:10 Roland 74.2k463103 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. errbar r –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 52 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="Measurements", ylab="Mean +/- SD", main="Scatter plot with std.dev error bars" ) # hack: we draw arrows but with very special "arrowheads" arrows(x, avg-sdev, x, avg+sdev, length=0.05, angle=90, code=3) The result looks like this: In the arrows(...) function length=0.05 is the size of the "arrowhead" in inches, angle=90 specifies that the "arrowhead" is perpendicular to the shaft of the arrow, and the particularly intuitive code=3 parameter specifies that we want to draw an arrowhead on both ends of the arrow. For horizontal error bars the following changes are necessary, assuming that
|| 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 factor or character variable
Ggplot2 Error Bars
(for horizontal error bars, x representing the group labels) y vector of summaryse r y-axis values. yplus vector of y-axis values: the tops of the error bars. yminus vector of y-axis values: the bottoms r arrows 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 of the plot. Defaults to 0.015. main http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars 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 line for error bars type type of point. Use type="b" http://svitsrv25.epfl.ch/R-doc/library/Hmisc/html/errbar.html 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 dotted reference line at zero is drawn. Author(s) Charles Geyer, University of Chicago. Modified by Frank Harrell, Vanderbilt University, to hand
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) http://monkeysuncle.stanford.edu/?p=485 Statistics (16) Teaching (10) Uncategorized (28) Meta Log in Entries RSS http://www.personality-project.org/r/html/error.bars.html 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 that there is no error bar 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) != length(y) | length(y) !=length(lower) | plot with error 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 reporting the results of scientific experiments: adjacent bars representing the treatment and t
boxplot to summarize distributions. Means and standard errors are calculated from the raw data using describe. Alternatively, plots of means +/- one standard deviation may be drawn. Usage error.bars(x,stats=NULL, ylab = "Dependent Variable",xlab="Independent Variable", main=NULL,eyes=TRUE, ylim = NULL, xlim=NULL,alpha=.05,sd=FALSE, labels = NULL, pos = NULL, arrow.len = 0.05,arrow.col="black", add = FALSE,bars=FALSE,within=FALSE, col="blue",...) Arguments x A data frame or matrix of raw data stats Alternatively, a data.frame of descriptive stats from (e.g., describe) ylab y label xlab x label main title for figure ylim if specified, the limits for the plot, otherwise based upon the data xlim if specified, the x limits for the plot, otherwise c(.5,nvar + .5) eyes should 'cats eyes' plots be drawn alpha alpha level of confidence interval – defaults to 95% confidence interval sd if TRUE, draw one standard deviation instead of standard errors at the alpha level labels X axis label pos where to place text: below, left, above, right arrow.len How long should the top of the error bars be? arrow.col What color should the error bars be? add add=FALSE, new plot, add=TRUE, just points and error bars bars bars=TRUE will draw a bar graph if you really want to do that within should the error variance of a variable be corrected by 1-SMC? col color(s) of the catseyes. Defaults to blue. ... other parameters to pass to the plot function, e.g., typ="b" to draw lines, lty="dashed" to draw dashed lines Details Drawing the mean +/- a confidence interval is a frequently used function when reporting experimental results. By default, the confidence interval is 1.96 standard errors of the t-distribution. If within=TRUE, the error bars are corrected for the correlation with the other variables by reducing the variance by a factor of (1-smc). This allows for comparisons between variables. The error bars are normally calculated from the data using the describe function. If, alternatively, a matrix of statistics is provided with column headings of values, means, and se, then those values will be used for the plot (using the stats option). However, in this case, the error bars will be one s.e. rather than a function of the alpha level. If sd is TRUE, then the error bars will represent one standard deviation from the mean rather than be a function of alpha and the standard errors. Value Graphic output showing the means + x Thes