Data visualization is a cornerstone of effective data analysis, allowing us to quickly identify patterns, trends, and anomalies. Among the most powerful tools for this in R is ggplot2, a package renowned for its elegant and highly customizable graphics. Boxplots, in particular, are excellent for summarizing the distribution of a dataset, highlighting its median, quartiles, and potential outliers. While these outliers often signal critical insights, there are specific scenarios where you might want to ignore outliers in ggplot2 boxplot visualizations. This might be to focus on the central data distribution, simplify a busy plot, or present data in a way that aligns with a specific analytical objective, assuming the outliers have been thoroughly investigated and deemed non-significant for the current context.
Understanding Outliers in Boxplots
Before deciding to ignore outliers, it’s crucial to understand what they represent within the context of a boxplot. A standard boxplot uses the interquartile range (IQR) to define its “box,” which spans from the first quartile (Q1) to the third quartile (Q3). The line inside the box indicates the median. Whiskers typically extend from the box to the most extreme data point within 1.5 times the IQR from Q1 and Q3. Any data point falling outside these whiskers is conventionally marked as an outlier.
These individual points, often plotted as dots, signify values that deviate significantly from the majority of the data. They could be genuine extreme values, measurement errors, or indicators of a different underlying process. For instance, in a dataset of house prices, an outlier might be a luxury mansion in an otherwise modest neighborhood. Understanding the source and implications of these extreme values is paramount before any visual alteration. Ignoring them without prior investigation can lead to misleading interpretations or overlooked critical information, as emphasized by statistical experts like John Tukey, who popularized the boxplot.
The decision to manipulate the visual representation of these points should always be driven by a clear analytical goal, not merely to “clean up” a plot. For robust exploratory data analysis, identifying these points is often the first step. However, for a presentation focused solely on typical distribution, removing visual clutter by ignoring these points can enhance clarity for the intended audience.
Method 1: Suppressing Outliers with coef = 0
One of the most straightforward ways to ignore outliers in a ggplot2 boxplot is by adjusting the coef argument within geom_boxplot(). By default, coef is set to 1.5, corresponding to the 1.5 IQR rule for whisker length. When you set coef = 0, the whiskers will extend to the minimum and maximum values of the data, effectively making all data points appear within the whiskers, thus suppressing the visual representation of individual outlier points.
This method doesn’t remove the outliers from your dataset; it merely alters how geom_boxplot draws the whiskers and points. It’s a purely visual adjustment. This approach is particularly useful when you want to show the full range of data, but without explicitly highlighting potential outliers as distinct points. It can make a plot cleaner, especially with datasets containing many minor outliers that might clutter the visualization. However, be mindful that by doing so, you are visually obscuring potential anomalies, which could be misconstrued if the audience isn’t aware of this modification.
Example: Using coef = 0
Consider a dataset like mtcars. To visualize the distribution of ‘mpg’ by ‘cyl’ and suppress outliers, you would use the following structure:
library(ggplot2) ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + geom_boxplot(coef = 0) + labs(title = "MPG Distribution by Cylinders (Outliers Suppressed)", x = "Number of Cylinders", y = "Miles Per Gallon") + theme_minimal()
This code snippet effectively extends the whiskers to cover all data points, ensuring no individual outlier points are plotted. It’s a quick and efficient way to achieve the desired visual outcome without altering your original data. For more advanced data visualization techniques in R, consider exploring interactive data dashboards to enhance your presentations.
Method 2: Filtering Data Before Plotting
Instead of merely suppressing the visual representation, you might choose to genuinely remove outliers from your dataset before passing it to ggplot2. This approach involves a pre-processing step where you identify and filter out the outlier observations based on a defined statistical criterion, such as the 1.5 IQR rule. This method modifies the actual data used for plotting, meaning the summary statistics (median, quartiles) calculated by geom_boxplot will reflect the data without the removed outliers.
This method is more impactful as it changes the underlying data presented in the plot. It’s often used when you’re confident that the outliers are erroneous, or when your analysis specifically targets the “typical” range of data, excluding extreme values. However, it’s critical to document this data manipulation clearly, as it can significantly alter the interpretation of your results. Always consider the potential bias introduced by removing data points and ensure this aligns with your analytical goals. A robust understanding of your data distribution is key here.
Calculating Outlier Bounds
To filter data, you first need to calculate the upper and lower bounds for outliers. This typically involves the 1.5 IQR rule. For each group (if you’re plotting by groups), you’d calculate Q1, Q3, and IQR, then define the bounds.
calculate_outlier_bounds <- function(data_vec) { q1 <- quantile(data_vec, 0.25) q3 <- quantile(data_vec, 0.75) iqr <- q3 - q1 lower_bound <- q1 - 1.5 iqr upper_bound <- q3 + 1.5 iqr return(list(lower = lower_bound, upper = upper_bound)) }
This function helps establish the thresholds for what constitutes an outlier for a given numeric vector. Remember that this calculation should ideally be performed per group if your boxplot segments data, as outlier definitions can vary between subgroups. This is a common practice in R statistical computing.
Filtering and Plotting
Once you have the bounds, you can filter your dataset. For grouped data, you’d apply this logic iteratively or using a grouped data operation from packages like dplyr. Hereβs how you might filter the mtcars dataset to remove outliers in ‘mpg’ for each ‘cyl’ group:
library(dplyr) Calculate bounds for each group and filter mtcars_filtered <- mtcars %>% group_by(cyl) %>% filter(mpg >= calculate_outlier_bounds(mpg)$lower & mpg <= calculate_outlier_bounds(mpg)$upper) %>% ungroup() Plotting the filtered data ggplot(mtcars_filtered, aes(x = factor(cyl), y = mpg)) + geom_boxplot() + labs(title = "MPG Distribution by Cylinders (Outliers Removed from Data)", x = "Number of Cylinders", y = "Miles Per Gallon") + theme_minimal()
This process ensures that the boxplot itself only displays the data points that fall within the calculated non-outlier range. This method provides a cleaner visualization focused on the central tendency and spread of the main data body, but comes with the responsibility of transparent data handling. More information on the nuances of outlier detection can be found in academic resources, such as those provided by The American Statistical Association.
Method 3: Customizing Outlier Aesthetics ----------------------------------------Sometimes, you don’t want to completely ignore or remove outliers but rather deemphasize them visually. ggplot2 provides Question & Answer :
How would I ignore outliers in ggplot2 boxplot? I don’t simply want them to disappear (i.e. outlier.size=0), but I want them to be ignored such that the y axis scales to show 1st/3rd percentile. My outliers are causing the “box” to shrink so small its practically a line. Are there some techniques to deal with this?
Edit Here’s an example:
y = c(.01, .02, .03, .04, .05, .06, .07, .08, .09, .5, -.6) qplot(1, y, geom="boxplot")
Use geom_boxplot(outlier.shape = NA) to not display the outliers and scale_y_continuous(limits = c(lower, upper)) to change the axis limits.
An example.
n <- 1e4L dfr <- data.frame( y = exp(rlnorm(n)), #really right-skewed variable f = gl(2, n / 2) ) p <- ggplot(dfr, aes(f, y)) + geom_boxplot() p # big outlier causes quartiles to look too slim p2 <- ggplot(dfr, aes(f, y)) + geom_boxplot(outlier.shape = NA) + scale_y_continuous(limits = quantile(dfr$y, c(0.1, 0.9))) p2 # no outliers plotted, range shifted
Actually, as Ramnath showed in his answer (and Andrie too in the comments), it makes more sense to crop the scales after you calculate the statistic, via coord_cartesian.
coord_cartesian(ylim = quantile(dfr$y, c(0.1, 0.9)))
(You’ll probably still need to use scale_y_continuous to fix the axis breaks.)
