Histogram of returns
A simple chart of returns does not reveal much about the time series properties; often, data must be displayed in a different format to visualize interesting features.
The density function, represented by the histogram of returns, indicates the most common returns in a time series without taking time into account. In R, these are calculated with the hist()
and density()
functions.
In the video, you saw how to create a histogram with 20 buckets, a title, and no Y axis label:
> hist(amazon_stocks,
breaks = 20,
main = "AMAZON return distribution",
xlab = "")
Recall that you can use the lines()
function to add a new time series, even with different line properties like color and thickness, to an existing plot.
In this exercise, you will create a histogram of the Apple daily returns data for the last two years contained in rtn
.
This exercise is part of the course
Visualizing Time Series Data in R
Exercise instructions
- Draw the histogram of
rtn
titled "Apple stock return distribution" and withprobability = TRUE
to scale the histogram to a probability density - Add a new line to the plot showing the density of
rtn
- Redraw the density line with twice the default width and a red color
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a histogram of Apple stock returns
# Add a density line
# Redraw a thicker, red density line