0

I am attempting to overlay boxplots over individual points on my scatterplot. However, I am having issues matching the axes on the two plots. Despite having the same number of elements (x axis) and value limit (y axis), the two axes of the two plots are scaled differently.

I am currently using:

plot((1:length(vec1)), vec1)
par(new=TRUE)
boxplot(mat2, names=c(1:length(vec1))) 

Does anyone know of a way of ensuring that the plots are on the same scale without explicitly coercing the xlim and ylim? (the dimensions of vec1 and mat2 change on iterations).

susan l
  • 3
  • 1
  • 1
    Can you please [provide a representative](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) sample of your data, e.g. `vec1` and `mat2`? – nrussell Aug 23 '14 at 14:08

1 Answers1

2

You can use the points function rather than calling plot.

For instance:

vec1 <- rnorm(10)
mat2 <- matrix(rnorm(1000), 100, 10)

boxplot(mat2, names=seq_along(vec1))
points(vec1)

This has also the advantage that the points are in front of the boxplot.

Note that you can retrieve current axis limits using par("usr"), although I can't seem to align the two plots properly even using those as xlim and ylim. I am guessing this depends on how boxplot works internally (haven't investigated that in depth though...)

nico
  • 50,859
  • 17
  • 87
  • 112
  • Thanks very much for the quick response. This works perfectly--I am just using `lines()` rather than `points()` due to the structure of my graph. – susan l Aug 23 '14 at 16:03
  • @susanl note that you can use `points` with `t="l"` as well (or any other graph type supported by `plot`) – nico Aug 23 '14 at 16:34