Get startedGet started for free

A fancy stock chart (2)

In this exercise, you will add a legend to the chart that you just created containing the name of the companies and the dates and values of the latest dividends.

Fill in the pre-written code with the following variables containing the dividend values and dates for both companies:

  • citi_div_value
  • citi_div_date
  • micro_div_value
  • micro_div_date

Recall that the default color of a plotted line is black, and that the values for legend, col, and lty in legend() should be set to vectors of the same length as the number of time series plotted in your chart.

If you can't see all of the legend, try making the chart full screen after you plot it. Let's make our chart even fancier!

This exercise is part of the course

Visualizing Time Series Data in R

View Course

Exercise instructions

  • Create the same plot as in the previous exercise (this has been done for you)
  • Use the pre-loaded variables above to create the strings, micro and citi, to be used in the legend
  • "Microsoft div of $0.39 on 15 Nov. 2016"
  • "Citigroup div of $0.16 on 13 Nov. 2016"
  • Create the legend on the bottom right corner of the chart using the micro and citi strings you just created for the text, appropriate colors for the labels, and regular lines

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Same plot as the previous exercise
plot(data$microsoft, main = "Dividend date and amount")
lines(data$citigroup, col = "orange", lwd = 2)
axis(side = 4, at = pretty(data$citigroup), col = "orange")

# Create the two legend strings
micro <- paste0("Microsoft div. of ", ___," on ", ___)
citi <- paste0("Citigroup div. of ", ___," on ", ___)

# Create the legend in the bottom right corner
legend(x = ___, legend = c(___, ___), col = c(___, ___), lty = c(___, ___))
Edit and Run Code