Obtain Historical Stock Data

To create this example, we need to obtain historical stock price data for Apple Inc. we can use packages like quantmod to fetch this data from Yahoo Finance or other sources. Install the package if you haven’t already:

R




install.packages("quantmod")
library(quantmod)
 
# Fetch Apple Inc. stock data for the past year
getSymbols("AAPL", from = Sys.Date() - 365, to = Sys.Date())
 
# Extract the closing prices from the data
apple_stock <- AAPL$AAPL.Close
 
# Create a date sequence for the x-axis
dates <- index(apple_stock)
 
# Create a step line plot
plot(dates, apple_stock, type = "s", lwd = 2, col = "blue",
     xlab = "Date", ylab = "Stock Price (USD)",
     main = "Apple Inc. Stock Price (Past Year)")
 
# Add grid lines
grid(lty = 3, col = "gray")
 
# Highlight significant events with vertical lines
abline(v = as.Date(c("2023-01-03", "2023-03-01", "2023-06-01", "2023-09-01")),
       lty = 2, col = "red")
 
# Add data points at significant events
points(as.Date(c("2023-01-03", "2023-03-01", "2023-06-01", "2023-09-01")),
       apple_stock[c(1, 60, 125, length(apple_stock))], pch = 19, col = "red")
 
# Add a legend
legend("topleft", legend = "Significant Events", col = "red", pch = 19, lty = 2)
 
# Add a horizontal line at the initial stock price
abline(h = apple_stock[1], lty = 2, col = "green")
 
# Annotate the initial stock price
text(dates[1], apple_stock[1], paste("Initial Price: $", round(apple_stock[1], 2)),
     pos = 4, col = "green", cex = 0.8)


Output:

  • We fetch real historical stock price data for Apple Inc.
  • We create a step line plot with a thick blue line to represent daily closing prices.
  • Grid lines are added to improve readability.
  • Vertical red dashed lines and red data points are used to highlight significant events (e.g., earnings reports, product launches).
  • A legend is added to explain the significance of the red lines and data points.
  • A green dashed horizontal line represents the initial stock price, and its value is annotated on the plot.

Step Line Plot in R

Data points are shown as a series of horizontal and vertical steps using step line plots, sometimes referred to as step plots or stair plots, which are a style of data visualisation used in R and other data analysis tools. These charts are especially helpful for displaying data, such as time series or cumulative data, that changes dramatically at precise times. In this post, we’ll look at how to make step-line graphs in R, alter how they look, and analyse the data they display.

Similar Reads

Key Features of Step Line Plots:

The Key Features of Step Line Plots are as follows:...

Creating a Basic Step Line Plot

R # Create random data x <- 1:10 y <- cumsum(runif(10))   # Create a step line plot plot(x, y, type = "s", lwd = 2, col = "blue", main = "Step Line Plot Example",      xlab = "X-axis", ylab = "Y-axis")...

Customizing the Step Line Plot

...

Create multiple step lines and add a legend

Step line plots can be customized in various ways to enhance their appearance and convey information effectively. Here are some common customization options:...

Obtain Historical Stock Data

...

Conclusion

...

Contact Us