- R Data Visualization Recipes
- Vitor Bianchi Lanzetta
- 83字
- 2021-07-02 23:33:34
How to do it...
We proceed as follows to add notches and jitters to box plots:
- Load the packages and use boxplot.stats() to separate the outliers:
> library(ggplot2) ; library(car)
> out_data <- Salaries[match((boxplot.stats(Salaries$salary))$out,
Salaries$salary),]
- Add geom_jitter() along with geom_boxplot() to draw a box plot with jittered outliers:
> set.seed(50)
> box1 <- ggplot(Salaries, aes( x = rank, y = salary))
> box1 + geom_boxplot( notch = T, outlier.shape = NA ) +
geom_jitter( data = out_data, height = 0, width = .2)
Following, figure 3.2 shows a notched box plot with jittered outliers:

Figure 3.2 Notched and jittered box plot
Move on to the next section for explanations.