Skip to main content

3.8) Covariance And Correlation: An Example


Try plot(GRASS) and plot(swiss) (or look at another dataset). How do these relate to the outputs of cor(GRASS) and cor(swiss)? Produce a panel of two scatter plots of variables of your choice from swiss and annotate them with appropriate correlation statistics and P-values of tests for association (after using hist() to check whether both variables in your plots appear to be normally distributed, as required by Pearson’s rho). To add text exactly where you want on a plot, use the function locator() as follows:
text(locator(1), "Pearson’s rho =… \n P =…")

(with your values in place of the ) – and then just click with your mouse where you want the text. (What does \n do?)

To copy a graph into another package, make sure the graph window is selected and just click the camera icon or use File > Copy to the clipboard. Then paste into the other package. Otherwise, you can write graphics files in formats such as PDF, TIFF, etc, using functions like pdf(), tiff(), etc.