0
votes

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).

1
Can you please provide a representative sample of your data, e.g. vec1 and mat2?nrussell

1 Answers

2
votes

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...)