6.1 C
London
Saturday, December 21, 2024
HomeStatistics TutorialRHow to Remove Outliers in Boxplots in R

How to Remove Outliers in Boxplots in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Occasionally you may want to remove outliers from boxplots in R.

This tutorial explains how to do so using both base R and ggplot2.

Remove Outliers in Boxplots in Base R

Suppose we have the following dataset:

data 

The following code shows how to create a boxplot for this dataset in base R:

boxplot(data)

To remove the outliers, you can use the argument outline=FALSE:

boxplot(data, outline=FALSE)

Boxplot with outlier removed in R

Remove Outliers in Boxplots in ggplot2

Suppose we have the following dataset:

data 

The following code shows how to create a boxplot using the ggplot2 visualization library:

library(ggplot2)

ggplot(data, aes(y=y)) +
  geom_boxplot()

To remove the outliers, you can use the argument outlier.shape=NA:

ggplot(data, aes(y=y)) +
  geom_boxplot(outlier.shape = NA)

ggplot2 boxplot with outliers removed

Notice that ggplot2 does not automatically adjust the y-axis.

To adjust the y-axis, you can use coord_cartesian:

ggplot(data, aes(y=y)) +
  geom_boxplot(outlier.shape = NA) +
  coord_cartesian(ylim=c(5, 30))

ggplot2 boxplot with no outliers

The y-axis now ranges from 5 to 30, just as we specified using the ylim() argument.

Additional Resources

The following tutorials explain how to perform other common operations in ggplot2:

How to Set Axis Limits in ggplot2
How to Create Side-by-Side Plots in ggplot2
How to Label Outliers in Boxplots in ggplot2

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories