'How can I use jitter horizontally on boxplot outliers?
I tried the following method from Outliers for boxplot and it worked well for me:
bar <- boxplot(foo,plot=FALSE)
boxplot(foo,outline=FALSE,ylim=c(min(c(bar$stats,bar$out)),max(c(bar$stats,bar$out))))
points(jitter(rep(1, length(bar$out))), bar$out)
What I need to do is use it with the boxplot parameter horizontal = TRUE. My boxplot does work horizontally but the jittered outliers are absent.
Any suggestions as to how to make this work? Thanks in advance.
Solution 1:[1]
You just need to change the x and y argument for points(). Note also how I simplified ylim.
set.seed(12345)
foo <- rnorm(1000)
bar <- boxplot(foo, plot = FALSE)
boxplot(foo, outline = FALSE,
ylim = range(foo),
horizontal = TRUE)
points(bar$out, jitter(rep(1, length(bar$out))))
Solution 2:[2]
In the multigroup setting, the following works:
p <- boxplot(
Sepal.Width ~ Species,
data = iris,
range = 0.5, # Just to get more outliers to show
outline = FALSE,
ylim = range(iris$Sepal.Width)
)
points(jitter(p$group, 0.1), p$out)

Created on 2022-02-14 by the reprex package (v2.0.1)
Note the use of p$group to get the x-coordinate of the outliers (rather than rep as in previous answers).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hplieninger |
| Solution 2 | MSR |
