Bar Plots In R With 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 <- app
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 to report the video? Sign in to report inappropriate content. Sign in
Calculate Standard Error In R
Transcript Statistics 2,204 views 5 Like this video? Sign in to make your opinion count. Sign errbar r in 6 1 Don't like this video? Sign in to make your opinion count. Sign in 2 Loading... Loading... Transcript The interactive transcript r arrows could not be loaded. Loading... Loading... Rating is available when the video has been rented. This feature is not available right now. Please try again later. Published on Oct 30, 2015 Category Education License Standard YouTube License Loading... Autoplay http://monkeysuncle.stanford.edu/?p=485 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,162 views 27:28 R Statistics tutorial: Creating bar charts for categorical variables | lynda.com - Duration: 9:06. Lynda.com 1,906 views 9:06 Learn R - Line Graphs with Error Bars in Ggplot2 - Duration: 34:05. Erin Buchanan 1,386 views 34:05 Plotting in R tutorial: Gorgeous graphs with ggplot2 - Duration: 32:05. https://www.youtube.com/watch?v=4Y3ZjFRwMX0 deltaDNA 24,170 views 32:05 Creating a Basic Bar Plot in R (Data Science & Statistics) - Duration: 24:34. Tim Layton 965 views 24:34 Creating Barplots in RStudio - Duration: 4:38. UTSSC 10,610 views 4:38 Excel Graphs With Error Bars Tutorial By Nestor Matthews - Duration: 14:12. Nestor Matthews 12,819 views 14:12 R tutorial: Creating bar charts for categorical variables | lynda.com - Duration: 13:21. Lynda.com 9,849 views 13:21 R Tutorial - Customizing Your Plots In R - Duration: 4:30. DataCamp 984 views 4:30 Add Error Bars to a Line Chart - Duration: 4:18. Doug H 93,185 views 4:18 Graphs and Error bars with Numbers - Duration: 7:07. Amanda M. Noller 9,346 views 7:07 Types of error-bars for mean: SD SE CI - Duration: 9:29. Ayumi Shintani 2,208 views 9:29 Creating Boxplots in RStudio - Duration: 4:17. UTSSC 19,844 views 4:17 Simple Graphs in R - Duration: 13:40. Christopher Hogue 50,797 views 13:40 Building a Line Plot in R - Duration: 5:06. Jeff Hamrick 14,747 views 5:06 Graphing: Origin 8.5.1: Add Error Bars to Graphs - Duration: 6:29. OriginLab Corp. 67,834 views 6:29 Introduction to Plotting in R - Duration: 5:02. Andrew Jahn 23,489 views 5:02 SPSS for newbies: How to create an error bar chart - Duration: 9:00. Phil Chan 29,582 views 9:00 Creating a Histogram in R Software (the hist() function) - Duration: 14:09. economicurtis 53,223 views 14:09 Adding standard error b
here for a quick overview of the site Help Center Detailed answers to http://stackoverflow.com/questions/29768219/grouped-barplot-in-r-with-error-bars any questions you might have Meta Discuss the workings and http://stackoverflow.com/questions/29995480/side-by-side-r-barplot-with-error-bars policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community error bar Stack Overflow is a community of 4.7 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. with error bars Here is 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
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 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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Side by Side R Barplot with error bars up vote 0 down vote favorite say I have the means of two datasets that I want to plot as barplots with error bars next to each other in ggplot2, or base Each dataset consists of a matrix of numbers 10 20 12 10 20 12 10 20 12 which is then transformed into a mean vector of for example 3 elements 10 20 12 What I want to do is to take both mean vectors and plot them as a bar plot where the first element of one is besides the first element of the other Dataset1Element1Bar-Dataset2Element1Bar Dataset1Element2Bar-Dataset2Element2Bar etc Give each bar an error bar, say of standard deviation. I know I can calculate it through sd but I'm not sure how to stick it into the graph in the proper form And lastly color them by their element number (ie Element 1) I have the code to do one dataset but I'm not sure where to go from there. result<-barplot(bardata, main="Mean Coverage", names.arg=namePosTargetGroup, ylab="mean Magnitude", cex.names=.4,col=c("red","blue","green")) legend(10,legend=c("Group1","Group2","Group3"),fill = c("red","blue","green")) A lot of what I look up gives the answer for one thing or another but its difficult to figure out how to combine them together. r graph plot ggplot2 share|improve this question asked May 1 '15 at 21:33 A D 328 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote I would generally not recommend plotting just a bar chart with error bars. There are many other ways to plot your data, which reveal the data and its structure a lot better. Especially if you just have very few cases, plotting means with bars is not good. A good explanation can be found here: Beyond Bar and Line Graphs: Time for a New Data Presentation Paradigm I find it difficult to give you a good solution, since I don't know your research-