Adding error bars to a line graph with ggplot2 in R
ggplot2 is an R language plotting package that creates complex plots from data in a data frame. It describes what variables to plot, how they are displayed, and general visual properties. It can add error bars, crossbars, line range, point range in our graph. This article is solely dedicated to adding error bars to line graphs.
- ymin or xmin : Lower Value of custom point
- ymax or xmax: Upper Value of custom point
- height: height of errorbar
- alpha: Opecity of error bar
- color: Color of error bar
- group: Differentiate points by group
Errorbar can be plotted horizontally or vertically by assigning xmin & xmax, ymin & ymax respectively.
Let’s start with a simple line graph
Now let us see how error bars are added to the line plot.
- Import library
- Create data
- Create plot
- Add functions to create an errorbar with specific parameter to obtain a desired result.
- Display plot
For Horizontal Errorplot, geom_errorbarh can be used and take parameters same as geom_errorbar but xmax & xmin is mandatory.
For Vertical Errorplot, assign ymin & ymax to geom_errorbar. The size parameter can change the width of error plot as shown below.
Sometimes, points are overlapped in the errorbar. For that position_dodge is used to move them horizontally.