R Barplot Error Bars
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) Meta Log in Entries RSS Comments RSS barplot with error bars ggplot2 WordPress.org ← Latest Swine Flu Epidemic Curve for the United States Stanford Workshop in
Error Bar In R
Biodemography → Plotting Error Bars in R August 24th, 2009 · 52 Comments · R One common frustration that I have heard error.bar function r expressed about R is 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. barplot with error bars matlab 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 it. First, I'll create 5 means drawn from a Gaussian random variable with unit mean and variance. I want to point
Summaryse R
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. control, male vs. female, etc.). Let's look at our same Gaussian means but now compare them to a Gaussian r.v. with mean 1.1 and unit variance. PLAIN TEXT R: y1 <- rnorm(500, mean=1.1) y1 <- matrix(y1,100,5) y1.means <- apply(y1,2,mean) y1.sd <- apply(y1,2,sd&
to create a barplot with SEM error bars Aubrie Holman SubscribeSubscribedUnsubscribe Loading... Loading... Working... Add to Want to watch this again later? Sign in to add this video to a playlist. Sign in Share More Report Need errbar r to report the video? Sign in to report inappropriate content. Sign in Transcript scatter plot with error bars in r Statistics 2,544 views 5 Like this video? Sign in to make your opinion count. Sign in 6 1 Don't like
R Arrows
this video? Sign in to make your opinion count. Sign in 2 Loading... Loading... Transcript The interactive transcript could not be loaded. Loading... Loading... Rating is available when the video has been rented. http://monkeysuncle.stanford.edu/?p=485 This feature is not available right now. Please try again later. Published on Oct 30, 2015 Category Education License Standard YouTube License Loading... Autoplay When autoplay is enabled, a suggested video will automatically play next. Up next Learn R - Bar Charts with Error Bars in Ggplot2 - Duration: 27:28. Erin Buchanan 3,616 views 27:28 R Statistics tutorial: Creating bar charts for categorical variables | lynda.com - http://www.youtube.com/watch?v=4Y3ZjFRwMX0 Duration: 9:06. Lynda.com 2,035 views 9:06 Learn R - Line Graphs with Error Bars in Ggplot2 - Duration: 34:05. Erin Buchanan 1,519 views 34:05 Creating Barplots in RStudio - Duration: 4:38. UTSSC 11,112 views 4:38 Plotting in R tutorial: Gorgeous graphs with ggplot2 - Duration: 32:05. deltaDNA 27,111 views 32:05 Creating a Basic Bar Plot in R (Data Science & Statistics) - Duration: 24:34. Tim Layton 1,008 views 24:34 How to Add Legends to Plots in R (R Tutorial 2.10) - Duration: 8:28. MarinStatsLectures 28,831 views 8:28 R tutorial: Creating bar charts for categorical variables | lynda.com - Duration: 13:21. Lynda.com 10,382 views 13:21 How to Produce Mean, Standard Deviation, Frequencies in R (Descriptive Statistics R Tutorial 2.7) - Duration: 6:50. MarinStatsLectures 74,254 views 6:50 Introduction to Plotting in R - Duration: 5:02. Andrew Jahn 24,340 views 5:02 Excel Graphs With Error Bars Tutorial By Nestor Matthews - Duration: 14:12. Nestor Matthews 15,604 views 14:12 Graphing in R 1: Intro to ggplot2 - Duration: 11:25. GordonAnthonyDavis 22,780 views 11:25 Add Error Bars to a Line Chart - Duration: 4:18. Doug H 96,577 views 4:18 Standard Deviation using R Programming - Statistics Tutorial - Duration: 3:39. Edward
here for a quick overview of the site Help Center Detailed answers to any http://stackoverflow.com/questions/29768219/grouped-barplot-in-r-with-error-bars 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 http://svitsrv25.epfl.ch/R-doc/library/Hmisc/html/errbar.html hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow error bar is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Grouped barplot in R with error bars up vote 4 down vote favorite 1 Dear Stackoverflow users, I would like to draw a grouped barplot with error bars. Here is with error bars the kind of figure I have been able to get up to now, and this is ok for what I need: And here is my script: #create dataframe Gene<-c("Gene1","Gene2","Gene1","Gene2") count1<-c(12,14,16,34) count2<-c(4,7,9,23) count3<-c(36,22,54,12) count4<-c(12,24,35,23) Species<-c("A","A","B","B") df<-data.frame(Gene,count1,count2,count3,count4,Species) df mean1<-mean(as.numeric(df[1,][c(2,3,4,5)])) mean2<-mean(as.numeric(df[2,][c(2,3,4,5)])) mean3<-mean(as.numeric(df[3,][c(2,3,4,5)])) mean4<-mean(as.numeric(df[4,][c(2,3,4,5)])) Gene1SpeciesA.stdev<-sd(as.numeric(df[1,][c(2,3,4,5)])) Gene2SpeciesA.stdev<-sd(as.numeric(df[2,][c(2,3,4,5)])) Gene1SpeciesB.stdev<-sd(as.numeric(df[3,][c(2,3,4,5)])) Gene2SpeciesB.stdev<-sd(as.numeric(df[4,][c(2,3,4,5)])) ToPlot<-c(mean1,mean2,mean3,mean4) #plot barplot plot<-matrix(ToPlot,2,2,byrow=TRUE) #with 2 being replaced by the number of genes! tplot<-t(plot) BarPlot <- barplot(tplot, beside=TRUE,ylab="count", names.arg=c("Gene1","Gene2"),col=c("blue","red")) #add legend legend("topright", legend = c("SpeciesA","SpeciesB"), fill = c("blue","red")) #add error bars ee<-matrix(c(Gene1SpeciesA.stdev,Gene2SpeciesA.stdev,Gene1SpeciesB.stdev,Gene2SpeciesB.stdev),2,2,byrow=TRUE)*1.96/sqrt(4) tee<-t(ee) error.bar(BarPlot,tplot,tee) The problem is that I need to do this for 50 genes, and 4 species, so my script is gonna get super super long and I guess this is not optimized... I tried to find help here but I can't figure out a better way to do what I'd like. If I did not need error bars I could adapt this script but the tricky part is to mix ggplot beautiful barplots and error bars! ;) I
|| 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 (for horizontal error bars, x representing the group labels) y vector of y-axis values. yplus vector of y-axis values: the tops of 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 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 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 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 handle missing data, to add the parameters add and lty, and to implement horizontal charts with differences. Examples set.seed(1) x <- 1:10 y <- x + rnorm(10) delta <- runif(10) errbar( x, y, y + delta, y - delta ) # Show bootstrap nonparametric CLs for 3