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?
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.
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 .
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).
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With