1. Other useful visualizing functions
Let's go back to our initial example
2. Other useful visualizing functions
and draw my_ts first.
3. Other useful visualizing functions
If you want to add an extra series to your existing chart, use the lines function. This function allows you to customize the new line with different color, thickness, line type and more. In this example you simply draw a new series my_ts2 and make it red.
4. Other useful visualizing functions
To make the chart more readable you might want to create a new Y-axis for the new series my_ts2. In this case you will use the axis function. The argument side equals 4 is the Y axis on the right hand side of the chart. Using the pretty-function inside the at-argument allows you to properly scale the new axis with minimal code.
5. Other useful visualizing functions
Adding a legend to a chart is also part of the most common tasks to master. In this case use the legend function. It can be largely customized. In particular you can choose the position of the legend and associate to it a certain line type or color or both matching the line type and color of the lines on your chart.
6. abline()-example
To highlight a certain point in the time series use abline. With abline you can highlight a certain date with the option v equals x where x is a date in the index of your time series. You can also highlight a certain value of your time series
7. abline()-example
with h equals y where y is a certain value of your time series.
8. Other useful visualizing functions
In this example you draw an horizontal line where the value of the stocks price equal to $10.
9. Other useful visualizing functions
You can go even further and highlight an entire period in a time series. There are several ways of achieving this but a quick an easy way is to use a specific package. The PerformanceAnalytics package and more specifically the chart-dot-TimeSeries function in this package. In this example the highlighted period is represented by a background in light blue. Like pretty much everything in R, this can be customized.
10. Let's practice!
Let's put all this to practice!