Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 : Adding two errorbars to each point in scatterplot

Tags:

r

ggplot2

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?

like image 771
Jens Nielsen Avatar asked Feb 10 '12 17:02

Jens Nielsen


People also ask

Do scatter plots have error bars?

The Scatter Plots with Error Bars procedure extends the capability of the basic scatter plot by allowing you to plot the variability in Y and X corresponding to each point. Each point on the plot represents the mean or median of one or more values for Y and X within a subgroup.

How do you add error bars to Ggplot?

Start by initializing ggplot with the summary statistics data: Specify x and y as usually. Specify ymin = len-sd and ymax = len+sd to add lower and upper error bars. If you want only to add upper error bars but not the lower ones, use ymin = len (instead of len-sd ) and ymax = len+sd .

How do you add error bars to points in R?

Error bars can be added to plots using the arrows() function and changing the arrow head. You can add vertical and horizontal error bars to any plot type. Simply provide the x and y coordinates, and whatever you are using for your error (e.g. standard deviation, standard error).


1 Answers

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)) 

enter image description here

like image 58
joran Avatar answered Oct 04 '22 07:10

joran