Add Whiskers to ggplot2 Boxplot in R (Example)

 

In this article, I’ll illustrate how to draw a ggplot2 Boxplot with error bars (i.e. whiskers) in R.

The post will contain the following contents:

Let’s get started…

 

Exemplifying Data, Packages & Default Graph

Have a look at the exemplifying data below.

set.seed(3470556)                              # Create example data
data <- data.frame(values = rnorm(100),
                   groups = letters[1:4])
head(data)                                     # Head of example data

 

table 1 data frame add whiskers ggplot2 boxplot

 

Table 1 visualizes the output of the RStudio console that got returned after running the previous R programming code and shows that our exemplifying data contains two variables called “values” and “groups”. The variable values is numerical and the variable groups is a character.

We also have to install and load the ggplot2 package, to be able to use the functions that are included in the package:

install.packages("ggplot2")                    # Install & load ggplot2 package
library("ggplot2")

Now, we can draw the data as shown below:

ggplot(data, aes(values, group = groups)) +    # Draw ggplot2 boxplot without whiskers
  geom_boxplot()

 

r graph figure 1 add whiskers ggplot2 boxplot

 

After running the previous syntax the ggplot2 Boxplot you can see in Figure 1 has been plotted.

 

Example: Add Whiskers to Boxplot Using geom = “errorbar” within stat_boxplot() Function

The following R syntax illustrates how to draw a Box-and-Whisker-Plot using the ggplot2 package.

To achieve this, we have to add the stat_boxplot function to our plot. Within this function, we have to specify the geom argument to be equal to “errorbar”.

Consider the R syntax below:

ggplot(data, aes(values, group = groups)) +    # Add whiskers to boxplot
  stat_boxplot(geom = "errorbar") + 
  geom_boxplot()

 

r graph figure 2 add whiskers ggplot2 boxplot

 

By running the previous R programming code we have managed to create Figure 2, i.e. a ggplot2 boxplot where we put error bars on top.

 

Video, Further Resources & Summary

Some time ago I have released a video on my YouTube channel, which shows the content of this article. You can find the video below.

 

The YouTube video will be added soon.

 

Also, you may have a look at the related posts on this website. A selection of posts is listed below.

 

Summary: At this point of the page you should have learned how to create a ggplot2 Boxplot with whiskers in the R programming language. Let me know in the comments, in case you have further questions. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on new articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top