plot() function - basic parameters
The plot.xts()
function is the most useful tool in the R time series data visualization artillery. It is fairly similar to general plotting, but its x-axis contains a time scale. You can use plot()
instead of plot.xts()
if the object used in the function is an xts
object.
Let's look at a few examples:
> # Basic syntax
> plot(mydata)
> # Add title and double thickness of line
> plot(mydata, main = "Stock XYZ", lwd = 2)
> # Add labels for X and Y axes
> plot(mydata, xlab = "X axis", ylab = "Y axis")
As you can see, there are a wide variety of parameters for the function allowing endless possibilities. Note that each call of plot()
creates an entirely new plot only using the parameters that are defined in that particular call.
Furthermore, to display the first few rows of a dataset mydata
to your console, use head(mydata)
. To display only the names of the columns, use colnames(mydata)
. You can also select a particular column of a dataset by specifying its title after a dollar sign, like in mydata$mycolumn
.
In this exercise, you will use the same dataset data
containing the daily stocks price for four major companies since 2015.
This exercise is part of the course
Visualizing Time Series Data in R
Exercise instructions
- Display the first few lines of the dataset
data
- Display the column names of the dataset
- Plot the first series of the data set and change the title to the name of the stock "yahoo"
- Replot the first series with the same title, and change the X label to "date" and Y label to "price"
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Display the first few lines of the data
# Display the column names of the data
# Plot yahoo data and add title
# Replot yahoo data with labels for X and Y axes