R Error Bars Using Arrows
Contents |
tutorials cover different topics including statistics, data manipulation and visualization! Introduction Getting Data Data Management Visualizing Data Basic Statistics Regression Models Advanced error bars in r barplot Modeling Programming Best R Packages Tips & Tricks Visualizing Data Building error bar in r Barplots with Error Bars by Chris Wetherill on August 17, 2015 3 Comments Bar charts are a pretty error.bar function r common way to represent data visually, but constructing them isn't always the most intuitive thing in the world. One way that we can construct these graphs is using R's default
Scatter Plot With Error Bars In R
packages. Barplots using base R Let's start by viewing our dataframe: here we will be finding the mean miles per gallon by number of cylinders and number of gears. View(mtcars) We begin by aggregating our data by cylinders and gears and specify that we want to return the mean, standard deviation, and number of observations for each group: myData <- errbar r aggregate(mtcars$mpg, by = list(cyl = mtcars$cyl, gears = mtcars$gear), FUN = function(x) c(mean = mean(x), sd = sd(x), n = length(x))) After this, we'll need to do a little manipulation since the previous function returned matrices instead of vectors myData <- do.call(data.frame, myData) And now let's compute the standard error for each group. We can then rename the columns just for ease of use. myData$se <- myData$x.sd / sqrt(myData$x.n) colnames(myData) <- c("cyl", "gears", "mean", "sd", "n", "se") myData$names <- c(paste(myData$cyl, "cyl /", myData$gears, " gear")) Now we're in good shape to start constructing our plot! Here, we'll start by widening the plot margins just a tad so that nothing runs off the edge of the figure (using the par() function). It's also a good habit to specify the upper bounds of your plot since the error bars are going to extend past the height of your bars. Beyond this, it's just any additional aesthetic styling that you want to tweak and you're good to go! The error bars are added in at the end using the segments() and arrows() functio
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 r arrows developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Error Bars In Ggplot2
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;
R Grouped Barplot With Error Bars
it only takes a 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 http://datascienceplus.com/building-barplots-with-error-bars/ 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 .. ... 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 http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars 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 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 o
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 https://stat.ethz.ch/pipermail/r-help/2000-November/009029.html 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) http://davetang.org/muse/2014/06/25/plotting-error-bars-with-r/ 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, error bar 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). > > error bars 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 ad
25, 2014 by Davo Error bars may show confidence intervals, standard errors, and standard deviations. Each feature conveys a different message and this paper on error bars in experimental biology explains it very nicely. For this post I will demonstrate how to plot error bars that show the standard error (SE) or standard error of the mean (SEM). I found two nice resources that demonstrate the plotting of error bars with R and in this post I illustrate them with simple examples. The first method is from the website of James Holland Jones, where he wrote an R function that plots arrows to a bar plot. #generate some random numbers set.seed(31) a <- runif(10, 0, 10) b <- runif(10, 0, 10) c <- runif(10, 0, 10) #store them as a data.frame df <- data.frame(a=a, b=b, c=c) #function for error bars 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, ...) } #function for standard error of the mean sem <- function(x){ sd(x)/sqrt(length(x)) } #calculate means my_mean <- apply(df, 2, mean) #calculate sem my_sem <- apply(df, 2, sem) #barplot barx <- barplot(my_mean, names.arg=names(df), ylim=c(0,ceiling(max(df))), xlab='Class', ylab='Unit of measure') error.bar(barx, my_mean, my_sem) The second resource I found on plotting error bars with R was from the Cookbook for R, which showed many examples using the R package ggplot2. Here is a simple example I adapted from their cookbook, using the same set of random numbers I generated above: #install if necessary install.packages('ggplot2') #load library library(ggplot2) set.seed(31) a <- runif(10, 0, 10) b <- runif(10, 0, 10) c <- runif(10, 0, 10) df <- data.frame(a=a, b=b, c=c) sem <- function(x){ sd(x)/sqrt(length(x)) } my_mean <- ap