If you still don’t use ggplot2 or, as I do, have to use the old and finicky plot() function, read on to discover a trick I use to display a legend outside the plotting area.
As en example, I am going to apply the principal component analysis method to the crabs dataset available in the MASS library. If you’re unfamiliar with the dataset I invite you to read this.
We get rid of the qualitative variables sp and sex because the PCA only applies to quantitative variables as well as the index column and the carapace length variable because of a previous analysis we had done which determined that this variable was the one which brought the least amount of information and was very correlated to the others and particularly the carapace width variable.
Moreover, we normalize the data thanks to the rowSums() function.
Next, we plot our PCA according to the first and second components:
Which gives us this:
We can clearly see the groups forming and with the help of the colouring we might be able to deduce which sex is which but since a plot is only as good as its legend, we are going to add one:
Notice particularly
which allows us to display stuff outside the plotting area. The default value for xpd which is NA means that the plot will cover the whole image. When specifying xpd = T, the plotting will be clipped to the figure region. We then extend the margins on our graph to give us space to display our legends with mar = par()$mar + c(0,0,0,7). Finally, we restore mar to its default value:
Which gives us a way better and more readable plot than before: