It is common to find yourself needing to use specific symbols or mathematical notation on R graphics. For example you may want to display R^2 values, but you also want the R^2 to be rendered nicely.
R has a rich set of options for including this mathematical text on plots. We previously discussed this in a tutorial.
Answers to the exercises are available here.
Exercise 1
Load the LakeHuron data set using the data()
function. This data includes the lake’s water depth in feet each year from 1875—1972.
Exercise 2
Plot Lake Huron water depth over time. Also use lm()
to run a linear model and plot a best fit line. Also, use summary()
to look at the model fit.
Exercise 3
Use str()
on the previous summary()
object to see everything you can extract from the summary()
object. Also, be able to extract the model coefficients specifically.
Exercise 4
Add text for a linear model (y[i] = alpha + beta*X[i]) on your plot using the text()
function combined with expression()
and paste()
- Work extensively with mathematical simulations,
- Learn the background math of these simulations and how mathematical expressions can simplify your work,
- And much more
Exercise 5
Again, add text detailing a linear model, but use the model coefficients in place of alpha and beta in the previous exercise. You will need to use substitute()
here.
Exercise 6
Add the R^2 symbol to the plot. Use expression()
.
Exercise 7
Use substitute()
to include the model estimated R^2 on the plot.
Exercise 8
In bold text, include R’s notation for a p-value, Pr(>|t|), in the title of the plot.
Exercise 9
Use the model estimated p-value for the significance of the slope of the line in the title of the plot.
Exercise 10
Use demo(plotmath) or ?plotmath to learn more about using mathematical notation in R
Leave a Reply