Horizontal Error Bars Ggplot2
Contents |
aes_string. Only needs to be set at the layer level if ggplot errorbar you are overriding the plot defaults. data A horizontal error bars r layer specific dataset - only needed if you want to override the geom_errorbar linetype plot defaults. stat The statistical transformation to use on the data for this layer. position The position adjustment to use for
Vertical Error Bars Ggplot2
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 geom_errorbar horizontal 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.
href='aes.html'>aes or aes_. If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping. data The data r calculate standard error to be displayed in this layer. There are three options: If NULL,
Summaryse
the default, the data is inherited from the plot data as specified in the call to ggplot. A
Ggplot Errorbarh
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify for which variables will be created. A function http://docs.ggplot2.org/0.9.3/geom_errorbarh.html will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data. stat The statistical transformation to use on the data for this layer, as a string. position Position adjustment, either as a string, or the result of a call to a position adjustment function. ... other arguments passed http://docs.ggplot2.org/current/geom_errorbarh.html on to layer. These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat. na.rm If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values. show.legend logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. inherit.aes If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders. Description Horizontal error bars Aesthetics geom_errorbarh understands the following aesthetics (required aesthetics are in bold): x xmax xmin y 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 = grou
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 http://stackoverflow.com/questions/9231702/ggplot2-adding-two-errorbars-to-each-point-in-scatterplot 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 http://www.mahalim.com/r-ggplot2-adding-both-vertical-and-horizontal-error-bars-in-each-point-of-a-scatter-plot/ Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ggplot2 : Adding two errorbars to each point in scatterplot up vote error bars 13 down vote favorite 6 I need to plot two error-bars on each point 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 horizontal error bars ggplot2 share|improve this question asked Feb 10 '12 at 17:03 Jens Nielsen 9515 1 I believe there's a geom_errorbarh that 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 horiz
be necessary to show both horizontal and vertical error bars/point in a scatterplot if both the dependent (Y) and independent (X) variables are averages of some other values. This is my personal note to keep things organized and ease of future use. I'd be happy if anyone is benefitted. Suppose, we have collected data of biomass and height of some plants and recorded the species names. Now we want to see how biomass is related to plant's height across species. To do so lets plot the average biomass in Y-axis and average height in X-axis in a scatter plot and fit a linear regression line across the species. We also want to draw error bars for each mean biomass and mean height pair. Here is how we can do:
# Create the dataset
biomass = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
height = c(0.15,.22,.34,.41,.5,.62,.71,.81,.92,1.01,1.1,1.25,1.32,1.41,1.52)
species = c('a','a','a','a','a','b','b','b','b','b','c','c','c','c','c') dummyData = data.frame(biomass,height,species) dummyData library(Rmisc) # Calculating SE (standard error) which will be used for error bars bmassStat = summarySE(dummyData,measurevar="biomass",groupvars = "species") bmassStat heightStat = summarySE(data=dummyData,measurevar = "height",groupvars = "species") heightStat # Combining variables and statistics bmassHt = data.frame(bmassStat$biomass,bmassStat$se,heightStat$height,heightStat$se,heightStat$species) names(bmassHt)=c("avgBiomass","biomassSE","avgHeight","heightSE","species") bmassHt library(ggplot2) ggplot(data=bmassHt,aes(x=avgHeight,y=avgBiomass,color=species))+ geom_smooth(method='lm',se=FALSE,color="black")+ geom_point()+ geom_errorbar(aes(ymax=avgBiomass+biomassSE,ymin=avgBiomass-biomassSE,width=0))+ geom_errorbarh(aes(xmax=avgHeight+heightSE,xmin=avgHeight-heightSE)) # geom_errobarh creates the horizontal error bar for the variable in the X-axis (in this case avgHeight) So, this is the final graph: Please let me know if you have any questions or suggestions. Sometimes it might be necessary to show both horizontal and vertical error bars/point in a scatterplot if both the dependent (Y) and independent (X) variables are averages of some