Ggplot2 Scatter Plot Error Bars
Contents |
aes_string. Only needs to be set at the layer level if ggplot2 error bars you are overriding the plot defaults. data A
R Calculate Standard Error
layer specific dataset - only needed if you want to override the
Ggplot Confidence Interval
plot defaults. stat The statistical transformation to use on the data for this layer. position The position adjustment to use for
Ggplot2 Stat_summary
overlappling points on this layer ... other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details. Description Horizontal error bars Aesthetics geom_errorbarh understands the following aesthetics (required summaryse aesthetics are in bold): x xmax xmin alpha colour height linetype size Examples df <- data.frame( trt = factor(c(1, 1, 2, 2)), resp = c(1, 5, 3, 4), group = factor(c(1, 2, 1, 2)), se = c(0.1, 0.3, 0.3, 0.2) ) # Define the top and bottom of the errorbars p <- ggplot(df, aes(resp, trt, colour = group)) p + geom_point() + geom_errorbarh(aes(xmax = resp + se, xmin = resp - se)) p + geom_point() + geom_errorbarh(aes(xmax = resp + se, xmin = resp - se, height = .2)) See also geom_errorbar: vertical error bars Back to top What do you think of the documentation? Please let me know by filling out this short online survey. Built by staticdocs. Styled with bootstrap.
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 geom_errorbar linetype more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags geom_errorbar horizontal Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, error bars in r helping each other. Join them; it only takes a minute: Sign up ggplot2 : Adding two errorbars to each point in scatterplot up vote 13 down vote favorite 6 I need to plot two error-bars on each point http://docs.ggplot2.org/0.9.3/geom_errorbarh.html in a scatterplot. The usual is vertical error-bars that corresponds to the error on the points y-value, but I need to add the error-bar associated with the X-axis (horizontal) as well. I could probably do this with some abline command, but thought there might be a more clever way to do it with ggplot2? r ggplot2 share|improve this question asked Feb 10 '12 at 17:03 Jens Nielsen 9515 1 I believe there's a geom_errorbarh that http://stackoverflow.com/questions/9231702/ggplot2-adding-two-errorbars-to-each-point-in-scatterplot takes x, xmin and xmax analogously to geom_errorbar. –joran Feb 10 '12 at 17:08 permalink.gmane.org/gmane.comp.lang.r.ggplot2/3231 –Ben Bolker Feb 10 '12 at 17:09 add a comment| 1 Answer 1 active oldest votes up vote 18 down vote accepted Just for completion's sake, following up on my comment, here is a simply (albeit ugly) example: df <- data.frame(x = 1:10, y = 1:10, ymin = (1:10) - runif(10), ymax = (1:10) + runif(10), xmin = (1:10) - runif(10), xmax = (1:10) + runif(10)) ggplot(data = df,aes(x = x,y = y)) + geom_point() + geom_errorbar(aes(ymin = ymin,ymax = ymax)) + geom_errorbarh(aes(xmin = xmin,xmax = xmax)) share|improve this answer answered Feb 10 '12 at 17:16 joran 102k11218272 2 Thanks a lot for that reply! it took me some time to reproduce your results with my own data as in my data the columns are NOT named "x" and "y", which (apparently) means that for the geom_errorbar you need to pass the x coordinate, that is: geom_errorbar(aes(x=var, ymin=...)) and for the geom_errorbarh both x and y, so: geom_errorbarh(aes(x=var1, y=var2, xmin=...)). This last detail of the horizontal geom_errorbarh does not seem to be documented in the help file, I had to deduce that from the error message I got. –Jens Nielsen Feb 13 '12 at 10:46 Sorry, I see that you define x and y in
by over 573 bloggers. There are many ways to follow us - By e-mail: On Facebook: If you are an R blogger yourself you are invited to add your own R content feed https://www.r-bloggers.com/building-barplots-with-error-bars/ to this site (Non-English R bloggers should add themselves- here) Jobs for R-usersFinance Manager @ Seattle, U.S.Data Scientist – AnalyticsTransportation Market Research Analyst @ Arlington, U.S.Data AnalystData Scientist for Madlan @ Tel Aviv, Israel Popular Searches web scraping heatmap twitter maps time series boxplot animation shiny how to import image file to R hadoop Ggplot2 trading latex finance eclipse excel quantmod sql googlevis PCA knitr rstudio ggplot market research rattle regression coplot map tutorial rcmdr error bars Recent Posts RcppAnnoy 0.0.8 R code to accompany Real-World Machine Learning (Chapter 2) R Course Finder update ggplot2 2.2.0 coming soon! All the R Ladies One Way Analysis of Variance Exercises GoodReads: Machine Learning (Part 3) Danger, Caution H2O steam is very hot!! R+H2O for marketing campaign modeling Watch: Highlights of the Microsoft Data Science Summit A simple workflow for deep learning gcbd 0.2.6 RcppCNPy 0.2.6 Using R to detect fraud at 1 million transactions per ggplot2 scatter plot second Introducing the eRum 2016 sponsors Other sites Jobs for R-users SAS blogs Building Barplots with Error Bars August 17, 2015By Chris Wetherill (This article was first published on DataScience+, and kindly contributed to R-bloggers) Bar charts are a pretty 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 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 <- 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