Skip to content

Update boxplot() to only plot points when < 5 data points #137

@hturner

Description

@hturner

Background

Currently boxplot() will create a boxplot even when there are fewer than 5 points, e.g.

set.seed(1)
x <- c(sample(1:10, 17, replace = TRUE), 50, 52)
g <- rep(1:2, c(4, 15))
x[g ==1]
#> [1] 9 4 7 1
bx <- boxplot(x ~ g)

The boxplot for each group is based on the output from boxplot.stats(), which uses stats::fivenum() internally

boxplot.stats(x[g ==1])
#> $stats
#> [1] 1.0 2.5 5.5 8.0 9.0
#> 
#> $n
#> [1] 4
#> 
#> $conf
#> [1] 1.155 9.845
#> 
#> $out
#> numeric(0)

boxplot() returns these stats along with the group index for outliers and the name of each group:

bx
#> $stats
#>      [,1] [,2]
#> [1,]  1.0  1.0
#> [2,]  2.5  4.0
#> [3,]  5.5  6.0
#> [4,]  8.0  9.5
#> [5,]  9.0 10.0
#> 
#> $n
#> [1]  4 15
#> 
#> $conf
#>       [,1]     [,2]
#> [1,] 1.155 3.756252
#> [2,] 9.845 8.243748
#> 
#> $out
#> [1] 50 52
#> 
#> $group
#> [1] 2 2
#> 
#> $names
#> [1] "1" "2"

Created on 2025-11-17 with reprex v2.1.1

Task

  1. Update boxplot.default() to plot points rather than a boxplot when there are fewer than five points (in a group). Specifically

    • Return all NA for the stats and conf elements
    • Return the data points as outliers in out instead, also updating group to identify those "outliers" as belonging to the respective group
    • For groups with NA stats, plot the outliers as points instead of a boxplot

    The code is in src/library/graphics/R/boxplot.R

  2. Update the help file to say that the data values are treated as outliers when there are fewer than 5 values.

  3. If time, run the tests for a package that depends on boxplot(), e.g. {ggplot2} , to check the impact.

Metadata

Metadata

Labels

GraphicsIssues related to graphicsRIssue should require knowledge of R onlyneeds patchImplement the agreed fix and prepare a patch for review

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions