Search code examples
ralignmentboxplot

Adding points to horizontal boxplots


I use the following code to produce multiple boxplots, ranked by the mean value of the variables:

zx <- replicate (5, rnorm(50))
zx_means <- (colMeans(zx, na.rm = TRUE))
colnames (zx) <- seq_len (ncol (zx))
boxplot(zx [, order (zx_means)], horizontal = FALSE, outline = FALSE)
points(zx_means [ order (zx_means)], pch = 22, col = "darkgrey", lwd = 7)

(See this post for more details)

When I change the code to horizontal = TRUE, I'm not able to make the points line up with the boxplots. Any ideas for how to properly add points to horizontal boxplots?


Solution

  • You need to give both x and y coordinates:

    points(zx_means[order (zx_means)], seq_along(zx_means), 
           pch = 22, col = "darkgrey", lwd = 7)
    

    or

    points(zx_means, order (zx_means), pch = 22, col = "darkgrey", lwd = 7)