Problem with "jitter"


#1

Hi,

I've plotted this boxplot:

boxplot(a, ylab="Explosions", main="Boxplot")
points(a[1], pch=20, col="blue")
points(a[2], pch=20, col="blue")
points(a[3], pch=20, col="blue")
points(a[4], pch=20, col="blue")
points(a[5], pch=20, col="blue")
points(a[6], pch=20, col="blue")
points(a[7], pch=20, col="blue")
points(a[8], pch=20, col="blue")
points(a[9], pch=20, col="blue")
points(a[10], pch=20, col="blue")

Where a <- c(5,15,10,7,9,11,13,9,10,12)

  1. I would like to know how to use the command jitter ...
    I've tried something like points(a[1], y=NULL, (jitter(a, factor=1, amount=NULL), pch=20, col="red") but it doesn't work... any idea?

  2. I would like to rotate the boxplot of 90°

Bye,
Bellico


#2

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

Right now the best way to install reprex is:

# install.packages("devtools")
devtools::install_github("tidyverse/reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.


#3

boxplot takes a horizontal parameter, which plots the values along the x axis instead of the y axis. You can plot all your points in one step if you supply a y vector the same length as your values. It's not documented in an obvious way, but the y coordinate for a single horizontal boxplot is 1.

jitter needs to be applied to a vector, so now that the boxplot is horizontal, jitter should be applied to the vector of y values (I boosted the factor here to make the jittering really obvious, but you might want different parameters):

a <- c(5, 15, 10, 7, 9, 11, 13, 9, 10, 12)

# Horizontal boxplot
boxplot(a, ylab = "Explosions", main = "Boxplot", horizontal = TRUE)

# Plot a in one step
points(x = a, y = rep(1, length(a)), pch = 20, col = "blue")

# Plot a with jitter in the y coordinates
points(x = a, y = jitter(rep(1, length(a)), factor = 5), pch = 20, col = "red")

8338


#4

Thanks! It's really usefull :slight_smile: