Ggplot2 Barplot With Error Bars
Contents |
error bars Two within-subjects variables Note about normed means Helper functions Problem You want to plot means and error bars for a dataset. Solution To make graphs with
R Calculate Standard Error
ggplot2, the data must be in a data frame, and in “long” ggplot2 stat_summary (as opposed to wide) format. If your data needs to be restructured, see this page for more information. Sample summaryse data The examples below will the ToothGrowth dataset. Note that dose is a numeric column here; in some situations it may be useful to convert it to a factor. class="n">tgGeom_errorbar Linetype
Ggplot Confidence Interval
to summarize the data. This can be done in a number of ways, as described on this page. In this case, we’ll use the summarySE() function defined on that page, and also at the bottom of this page. (The code for the summarySE function must be entered before it is called here). # summarySE provides the standard deviation, standard error of the mean, and a (default 95%) confidence interval tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","dose")) tgc #> supp dose N len sd se ci #> 1 OJ 0.5 10 13.23 4.459709 1.4102837 3.190283 #> 2 OJ 1.0 10 22.70 3.910953 1.2367520 2.797727 #> 3 OJ 2.0 10 26.06 2.655058 0.8396031 1.899314 #> 4 VC 0.5 10 7.98 2.746634 0.8685620 1.964824 #> 5 VC 1.0 10 16.77 2.515309 0.7954104 1.799343 #> 6 VC 2.0 10 26.14 4.797731 1.5171757 3.432090 Line graphs After the data is summarized, we can make the graph. These are basic line
tutorials cover different topics including statistics, data manipulation and visualization! Introduction Getting Data Data Management Visualizing Data Basic Statistics Regression Models Advanced Modeling Programming Best R Packages Tips & Tricks error bars in r Visualizing Data Building Barplots with Error Bars by Chris Wetherill on August 17, 2015
Barplot With Error Bars R
3 Comments Bar charts are a pretty common way to represent data visually, but constructing them isn't always the most geom_errorbar horizontal intuitive thing in the world. One way that we can construct these graphs is using R's default packages. Barplots using base R Let's start by viewing our dataframe: here we will be finding the http://cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ 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 <- 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 http://datascienceplus.com/building-barplots-with-error-bars/ 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() functions. In this case, we are extending the error bars to ±2 standard errors about the mean. par(mar = c(5, 6, 4, 5) + 0.1) plotTop <- max(myData$mean) + myData[myData$mean == max(myData$mean), 6] * 3 barCenters <- barplot(height = myData$mean, names.arg = myData$names, beside = true, las = 2, ylim = c(0, p
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://stackoverflow.com/questions/15064462/r-ggplot2-barplot-and-error-bar 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 https://heuristically.wordpress.com/2013/10/20/bar-plot-with-error-bars-r/ 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 error bars takes a minute: Sign up R: ggplot2 barplot and error bar up vote 2 down vote favorite Since the new version of ggplot2 (0.9.3), I've problem to plot barplots with errorbars. So I've a dataframe like this : group N val sd se ci 1 206 3 37.2269533 7.9688645 4.6008261 19.7957568 2 207 3 2.0731505 2.2843009 1.3188417 5.6745180 3 208 3 barplot with error 2.2965978 1.4120606 0.8152536 3.5077531 4 209 3 3.1085132 1.1986664 0.6920504 2.9776525 5 210 3 3.3735251 1.9226134 1.1100214 4.7760365 6 211 3 4.0477951 2.9410503 1.6980162 7.3059739 7 212 3 1.2391158 1.2345554 0.7127709 3.0668055 8 213 2 1.3082374 1.1234220 0.7943793 10.0935460 I want to plot for each group the val +- s : I did that before upgrade : ggplot(dfc, aes(x=factor(group), y=factor(val)) + geom_bar(position=position_dodge()) + geom_errorbar(aes(ymin=val-se, ymax=val+se),width=.1,position=position_dodge(.9)) It gives me that: Mapping a variable to y and also using stat="bin". With stat="bin", it will attempt to set the y value to the count of cases in each group. This can result in unexpected behavior and will not be allowed in a future version of ggplot2. If you want y to represent counts of cases, use stat="bin" and don't map a variable to y. If you want y to represent values in the data, use stat="identity". See ?geom_bar for examples. (Deprecated; last used in version 0.9.2) So anyone knows how to resolve that ? Thanks N. r ggplot2 share|improve this question asked Feb 25 '13 at 10:06 NicoBxl 3374515 add a comment| 1 Answe
standard error of the mean, and a 95% confidence interval. The key step is to precalculate the statistics for ggplot2. n.per.group<-10 alpha<-0.05 # for a (1.00-alpha)=95% confidence interval # Simulate raw data for an experiment or observational study. data.raw <- data.frame( treatment=rep(c('A','B'), each=n.per.group), value=c(rnorm(n.per.group, 2), rnorm(n.per.group, 3)) ) # This data frame calculates statistics for each treatment. data.summary <- data.frame( treatment=levels(data.raw$treatment), mean=tapply(data.raw$value, data.raw$treatment, mean), n=tapply(data.raw$value, data.raw$treatment, length), sd=tapply(data.raw$value, data.raw$treatment, sd) ) # Precalculate standard error of the mean (SEM) data.summary$sem <- data.summary$sd/sqrt(data.summary$n) # Precalculate margin of error for confidence interval data.summary$me <- qt(1-alpha/2, df=data.summary$n)*data.summary$sem # Load ggplot2 library require(ggplot2) # Use ggplot to draw the bar plot using the precalculated 95% CI. png('barplot-ci.png') # Write to PNG ggplot(data.summary, aes(x = treatment, y = mean)) + geom_bar(position = position_dodge(), stat="identity", fill="blue") + geom_errorbar(aes(ymin=mean-me, ymax=mean+me)) + ggtitle("Bar plot with 95% confidence intervals") + # plot title theme_bw() + # remove grey background (because Tufte said so) theme(panel.grid.major = element_blank()) # remove x and y major grid lines (because Tufte said so) dev.off() # Close PNG # Plot one standard error (standard error of the mean/SEM) png('barplot-sem.png') ggplot(data.summary, aes(x = treatment, y = mean)) + geom_bar(position = position_dodge(), stat="identity", fill="blue") + geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) + ggtitle("Bar plot with standard error as error bars") + theme_bw() + theme(panel.grid.major = element_blank()) dev.off() # Plot one standard deviation png('barplot-sd.png') ggplot(data.summary, aes(x = treatment, y = mean)) + geom_bar(position = position_dodge(), stat="identity", fill="blue") + geom_errorbar(aes(ymin=mean-sd, ym