Error Bars using ggplot2 in R
Last Updated :
28 Jul, 2021
Error bars are bars that show the mean score. The error bars stick out from the bar like a whisker. The error bars show how precise the measurement is. It shows how much variation is expected by how much value we got. Error bars can be plated both horizontally and vertically. The horizontal error bar plot shows error bars for group differences as well as bars for groups.
The error bar displays the precision of the mean in one of 3 ways:
- The confidence interval
- The standard error of the mean
- Standard Deviation
geom_errorbar()
Various ways of representing a vertical interval are defined by x, ymin, and ymax. Each case draws a single graphical object. Here geom draws the error bar, which can be defined by the lower and upper values. Remember that you have to provide the value of y_min and y_max ourselves because the error bar geom doesn't compute the confidence level automatically.
Syntax :
geom_errorbar(mapping = NULL,data = NULL,stat = "identity",position = "identity",na.rm = FALSE, orientation = NA,show.legend = NA,inherit.aes = TRUE)
Example :
R
library("ggplot2")
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line(aes(group = len/2))+geom_errorbar(
aes(ymin =len , ymax = dose),width=0.3)
Output :

geom_linerange()
Here geom draws the error bar, which can be defined by the lower and upper values. The linerange function is a bit similar to the error bar. In the line range function, you have to provide the value of y_min and y_max ourselves because the linerange geom doesn't compute the confidence level automatically. In geom_linerange there are some parameters that are by default present (size, line range, color, width).
Syntax :
geom_linerange(mapping = NULL,data = NULL,stat = "identity",position = "identity",na.rm = FALSE orientation = NA,show.legend = NA,inherit.aes = TRUE)
Example :
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_linerange(aes(ymin =len , ymax = dose)
)
Output :

geom_pointrange()
Here geom draws the error bar, which can be defined by the lower and upper values. The point range function is quite similar to the error bar and line range. In the point range function, you have to provide the value of y_min and y_max ourselves because the pointrange geom doesn't compute confidence level automatically. In geom_pointrange there are some parameters that are by default present (size, line range, color, fill, width). The pointrange function is useful to draw confidence intervals.
Syntax :
geom_linerange(mapping = NULL,data = NULL,stat = "identity",position = "identity",fatten = 4,na.rm = FALSE orientation = NA,show.legend = NA,inherit.aes = TRUE)
Example :
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_pointrange(aes(ymin =len , ymax = dose)
)
Output :

geom_crossbar()
Here geom draws the error bar, which can be defined by the lower and upper values. The crossbar function is quite similar to error bar and line range. The crossbar is the hollow bar with the horizontal lines in the middle.
Syntax:
geom_crossbar(mapping = NULL,data = NULL,stat = "identity",position = "identity",fatten = 2.5,na.rm = FALSE,orientation = NA,show.legend = NA,inherit.aes = TRUE)
Example :
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +geom_crossbar(aes(ymin = len , ymax = dose),width = 0.2)
Output :

geom_errorbarh()
Here geom draws the error bar, which can be defined by the lower and upper values. The geombar function is a bit similar to the error bar but here we are changing the position and axis. In the errorbar range function, you have to provide the value of x_min and x_max ourselves because the error bar geom doesn't compute the confidence level automatically. In geom_errorbar there are some parameters that are by default present (size, line range, color, width).
Syntax:
geom_errorbarh(mapping = NULL,data = NULL,stat = "identity",position = "identity",na.rm = FALSE,orientation = NA,show.legend = NA,inherit.aes = TRUE)
Example :
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(len,dose))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,
fill="green",color="red") +
geom_errorbarh(aes(xmin =dose , xmax = len)
)
Output :
Let us implement these on one variable system.
Data : example.csv
Load your data in a variable. Check if data is proper or not, if it's proper keep the same otherwise change according to your need. To plot the mean arousal score (y-axis) for the film (x-axis) first create the plot object.
Syntax :
factor(x = character(), levels, labels = levels,exclude = NA, ordered = is.ordered(x), nmax = NA)
If there is a need to change, the data conversion is done by setting them to actual factors. Then plot the data.
Example 1:
R
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2), l
abels = c("Bridget Jones","Memento"))
rawdata_bar = ggplot(rawdata,aes(film,arousal))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black")
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
Output:
Example 2: The same can be done for two independent variables.
R
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata2 = ggplot(rawdata,aes(film, arousal, fill = gender))
rawdata2 +
stat_summary(fun.y = mean, geom = "bar",position = "dodge")+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
position = position_dodge(width = 0.90),width=.2)
Output:
Similar Reads
Plot from DataFrame in ggplot2 using R
ggplot2 is a popular data visualization library in the R programming language. It is widely used for creating beautiful, customizable, and informative visualizations. One of the most useful features of ggplot2 is the ability to plot data stored in a data frame. In this article, we will learn how to
4 min read
Coloring Barplots with ggplot2 in R
In this article, we will discuss how to color the barplot using the ggplot2 package in the R programming language. Method 1: Using fill argument within the aes function Using the fill argument within the aes function to be equal to the grouping variable of the given data. Aesthetic mappings describe
2 min read
Stacked Bar Chart in R ggplot2
A stacked bar plot displays data using rectangular bars grouped by categories. Each group represents a category, and inside each group, different subcategories are stacked on top of each other. It also shows relationships between groups and subcategories, making them useful for various data analysis
4 min read
How to change background color in R using ggplot2?
In this article, we will discuss how to change the background color of a ggplot2 plot in R Programming Language. To do so first we will create a basic ggplot2 plot. Step 1: Create sample data for the plot. sample_data <- data.frame(x = 1:10, y = 1:10) Step 2: Load the package ggplot2. library("gg
2 min read
Working with Axes in R using ggplot2
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in the R Programming Language. The package can be downloaded and installed into the working space using t
7 min read
Remove Axis Labels using ggplot2 in R
In this article, we are going to see how to remove axis labels of the ggplot2 plot in the R programming language. We will use theme() function from ggplot2 package. In this approach to remove the ggplot2 plot labels, the user first has to import and load the ggplot2 package in the R console, which i
2 min read
Line Plot in R with Error Bars
A line plot is a graphical representation of a series of data. Line plots are widely used in data visualization. In we use ggplot2( ) and plot( ) function to create line plot. Error bars act as a visual enhancement that help us to see variability of the plotted data. Error Bars are used to show stan
2 min read
How to Remove Option Bar from ggplotly Using R
In interactive visualizations created with the plotly package in R, a toolbar appears by default when the plot is rendered. This toolbar allows users to zoom, pan, save images, and reset the view. However, in certain cases, you may want to remove this toolbar (also known as the options bar) to provi
2 min read
Draw ggplot2 Barplot With Round Corners in R
In this article, we will be looking at the various approaches to drawing ggplot2 barplot with round corners in the R programming language. Method 1: Draw ggplot2 Barplot with Round Corners Using ggchicklet Package In this approach to drawing a ggplot2 barplot with round corners, the user needs to fi
2 min read
How To Make Barplots with Error bars in ggplot2 in R?
In this article, we will discuss how to make a barplot with an error bar using ggplot2 in the R programming language. Error Bars helps us to visualize the distribution of the data. Error Bars can be applied to any type of plot, to provide an additional layer of detail on the presented data. Often t
4 min read