Visualize your vector
Time to try something a bit different. So far, you have been programming in the script, and looking at your data by printing it out. For a more informative visualization, try a plot!
For this exercise, you will again be working with some Apple stock data. This time it contains the prices for all of December, 2016.
The plot()
function is one of the many ways to create a graph from your data in R. Passing in a vector will add its values to the y-axis of the graph, and on the x-axis will be an index created from the order that your vector is in.
Inside of plot()
, you can change the type of your graph using type =
. The default is "p"
for points, but you can also change it to "l"
for line.
This is a part of the course
“Introduction to R for Finance”
Exercise instructions
apple_stock
has already been defined, and everything has been set up for you. Try running the script line-by-line using Command + Enter
on Mac or Control + Enter
on Windows while clicked on each line.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Look at the data
apple_stock
# Plot the data points
plot(apple_stock)
# Plot the data as a line graph
plot(apple_stock, type = "l")
This exercise is part of the course
Introduction to R for Finance
Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.
In this chapter, you will learn all about vectors and matrices using historical stock prices for companies like Apple and IBM. You will then be able to feel confident creating, naming, manipulating, and selecting from vectors and matrices.
Exercise 1: What is a vector?Exercise 2: c()ombineExercise 3: Coerce itExercise 4: Vector names()Exercise 5: Visualize your vectorExercise 6: Vector manipulationExercise 7: Weighted average (1)Exercise 8: Weighted average (2)Exercise 9: Weighted average (3)Exercise 10: Vector subsettingExercise 11: Matrix - a 2D vectorExercise 12: Create a matrix!Exercise 13: Matrix <- bind vectorsExercise 14: Visualize your matrixExercise 15: cor()relationExercise 16: Matrix subsettingWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.