Create new columns with month and day for easier subsetting
# Convert the date column (currently a character) to a Date format
amazon$Date <- as.Date(amazon$Date, format = "%m/%d/%Y")
target$Date <- as.Date(target$Date, format = "%m/%d/%Y")
# Create new column to represent the month as a number for each company
#AMAZON
amazon$month <- format(amazon$Date, "%m") # Extract the month
amazon$month <- as.numeric(amazon$month) # Convert to numeric
#TARGET
target$month <- format(target$Date, "%m") # Extract the month
target$month <- as.numeric(target$month) # Convert to numeric
# Create new column to represent the day as a number for each company
#AMAZON
amazon$day <- format(amazon$Date, "%d") # Extract the day value
amazon$day <- as.numeric(amazon$day) # Convert to numeric
# TARGET
target$day <- format(target$Date, "%d") # Extract the day value
target$day <- as.numeric(target$day) # Convert to numeric
# Since the datasets were from the last 6 months, we do not need to worry about year. It will be 2024 for any January values. We can now subset to select only the data points from January, with their day numbers and closing stock prices.
# January 2024 closing stock prices for Amazon
amazon_new <- subset(amazon, month == 1, select = c(day, Close.Last))
# January 2024 closing stock prices for Target
target_new <- subset(target, month == 1, select = c(day, Close.Last))
# Since the datasets were from the last 6 months, we do not need to worry about year. It will be 2024 for any January values. We can now subset to select only the data points from January, with their day numbers and closing stock prices.
# January 2024 closing stock prices for Amazon
amazon_new <- subset(amazon, month == 1, select = c(day, Close.Last))
# January 2024 closing stock prices for Target
target_new <- subset(target, month == 1, select = c(day, Close.Last))
# Analyze summary stats to know bounds for graphing
summary(amazon_new$Close.Last) # Amazon has higher max. closing price for Jan. 2024
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 144.6 151.4 154.6 153.6 156.0 161.3
summary(target_new$Close.Last) # target has lower min. closing price for Jan. 2024
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 137.4 139.2 140.9 140.8 142.0 144.1
# Find the day of the lowest closing price for Target
targ_min <- target_new$day[which.min(target_new$Close.Last)]
# Find the day of highest closing price for Amazon
amazon_max <- amazon_new$day[which.max(amazon_new$Close.Last)]
targ_min # 18 (Jan 18)
## [1] 18
amazon_max # 29 (Jan 29)
## [1] 29
# Find the closing prices corresponding to the lowest price for Target and highest price for Amazon; these two points will be labeled as the overall low and high values
min_price <- target_new$Close.Last[target_new$day == targ_min]
max_price <- amazon_new$Close.Last[amazon_new$day == amazon_max]
# Add extra lines around the margins, especially to the right side to show the entire legend; we will also want a few extra lines on the bottom for the subtitle
par(mar=par()$mar+c(2,1,0,7))
# Create plot <- starts with plotting closing price for Amazon against day in January
# Adds x and y labels and title
# Major tick marks plotted for x and y axis with xaxp and yaxp
# Makes title a bit bigger
plot(amazon_new$day, amazon_new$Close.Last, xlim = c(1,31), xaxp = c(1, 31, 6), yaxp = c(135,165,3), ylim = c(135,165),
main = "Daily Closing Stock Price for Amazon and Target in January of 2024",
xlab = "Day of January", ylab = "Closing Stock Price ($)", cex.main = 1.5, cex.lab = 1.4, cex.axis = 1.4)
# Next, grid is added with abline
# Gridlines match up with major axis marks
# Vertical grid
abline(v = seq(1, 31, 5),
lty = 2, col = "lightgray")
# Horizontal grid
abline(h = seq(135, 165, 10),
lty = 2, col = "lightgray")
# Since we added a grid, we now need to add the points, connected with lines to put OVER the grid or else they will be a bit hidden by the gridlines
# type = "o" connects the points and lines
# Add a color for each line and set the line width to two for each line
# Amazon points and lines (BLUE)
points(amazon_new$day, amazon_new$Close.Last, pch = 16, type = "o", col = "blue", lwd = 2, cex = 1.2)
# Target's closing stock price points, connected by a line (RED)
points(target_new$day, target_new$Close.Last, pch = 16, type = "o", col = "red", lwd = 2, cex = 1.2)
# Add minor tick marks on x-axis
axis(1, at = c(2:30), labels = FALSE, tck = -0.01)
# Add minor tick marks to left y-axis
axis(2, at = seq(140, 160, 10), labels = FALSE, tck = -0.01)
# For the top and right axes, major and minor tick marks will be added for ease with viewing, but labels are not necessary
# Add major tick marks, unlabeled, to top axis
axis(3, at = seq(1,31, by = 5), labels = FALSE)
# Add minor tick marks, unlabeled, to top axis
axis(3, at = c(2:30), labels = FALSE, tck = -0.01)
# Add major tick marks to right-axis
axis(4, at = seq(135,165,10), labels = FALSE)
# Add minor tick marks to right-axis
axis(4, at = seq(140, 160, 10), labels = FALSE, tck = -0.01)
# Subtitle describing lowest closing price for Target and when it occurred
mtext(paste0("* The lowest closing price was Target's $", min_price, " on ",
format(target$Date[target$Close.Last == min_price], "%m/%d/%Y")), side = 1, line = 5, adj = 0, cex = 1.3, font = 3)
# Subtitle describing highest closing price for Amazon and when it occurred
mtext(paste0("** The highest closing price was Amazon's $",max_price, " on ", format(amazon$Date[amazon$Close.Last == max_price], "%m/%d/%Y")), side = 1, line = 6, adj = 0,
cex = 1.3, font = 3)
# Add a legend outside the plot in the top-right.
# xpd = TRUE to set the legend outside, and inset is negative to put the legend a bit to the right of the plot
# Amazon = blue; Target = Red
legend(x = "topright", inset = c(-0.2, 0), legend = c("Amazon", "Target"),
col = c("blue", "red"), lwd = 2, xpd = TRUE)
# Label the low closing price and high closing price on the graph (under the low point and above the high point)
text(targ_min, min_price - 0.3, label = "Overall Low: $137.40", col = "red", font = 2, cex = 1.2, pos = 1) # pos = 1 places label below
text(amazon_max - 2, max_price + 0.2, label = "Overall High: $161.26", col = "blue", font = 2, cex = 1.2, pos = 3) # pos = 3 places label above
