8.4 Add Lines To Figure
Suppose you want to extend the above analysis to consider three different values of \(i\) 0.03, 0.05, and 0.07, and to visually compare the graphs on one plot.
First, you calculate the values:
<- 0.03
i2 <- 1000*compound(i2, t)
A2
<- 0.07
i3 <- 1000*compound(i3, t) A3
The following block of code repeats the same plot command as to what you did previously and includes the lines() function to add the plots when \(i = 0.03, 0.07\). In addition, a legend is included.
plot(t, A1, ylim = c(1000,2000),
type = "l", xlab = "Time (in years)",
ylab = "Accumulated Value (in $)",
main = "Accumulated Value of $1000 Investment", col = "black")
lines(t, A2, type = "l", col = "blue", lty = 2, lwd = 2)
lines(t, A3, type = "l", col = "red", lty = 3, lwd = 3)
legend("bottomright",
legend = c("3% Interest", "5% Interest", "7% Interest"),
col = c("blue", "black", "red"),
lty = c(2, 1, 3), bty = "o", cex = 0.75)
The y-axis now ranges from 1000 to 2000. See what happens to the plot if you delete the parameter ylim = c(1000,2000), from the plot above.
The lines are distinguished not only by color, but by line type, and line thickness. You can explore which parameters result in the appearance of the plot lines. The legend() function associates each line by color and type with the value of i. The “bty” option framed the legend, while the “cex” option determined the font size of the legend.