##This project is to look into Amazon’s monthly stock price changes happend from 1997 to 2024. For this, line chart, bar chart, candlestick chart and animation was used.

Load necessary libraries

library(ggplot2)
library(gganimate)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggthemes)
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dplyr)
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Reading the file :

stockdata<-read.csv("AMAZON_monthly.csv")
View(stockdata)
str(stockdata)
## 'data.frame':    321 obs. of  7 variables:
##  $ Date     : chr  "1997-06-01" "1997-07-01" "1997-08-01" "1997-09-01" ...
##  $ Open     : num  0.0755 0.0771 0.1172 0.1172 0.2219 ...
##  $ High     : num  0.0854 0.1286 0.1208 0.2406 0.275 ...
##  $ Low      : num  0.0688 0.0755 0.0969 0.1156 0.176 ...
##  $ Close    : num  0.0771 0.1198 0.1169 0.2169 0.2542 ...
##  $ Adj.Close: num  0.0771 0.1198 0.1169 0.2169 0.2542 ...
##  $ Volume   : num  1.06e+09 2.17e+09 6.38e+08 2.16e+09 2.10e+09 ...

Convert Date column to Date type

stockdata$Date <- as.Date(stockdata$Date)

Check for and handle missing values (if any)

missing_values <- sum(is.na(stockdata))
if (missing_values > 0) {
   stockdata <- na.omit(stockdata)
}

Interactive Visualization

Visualization 1: Trend of Stock Price (Line Chart)

a <-ggplot(stockdata, aes(x = Date, y = Close)) +
   geom_point(colour = "#146eb4", size = .4)+
   geom_line() +
   labs(title = "Trend of Stock Price",
        y = "Stock Price",
        x = "Date") +
   theme_minimal()
ggplotly(a)

##Explanation: The line chart clearly shows that the Amazon’s stock were not doing that well in the market from 1997 to 2009. However the stock price clearly saw a boom from 2010. The reasons considered for that are: -E-commerce’s continued growth -Amazon’s expansion to new areas -Amazon’s first reported annual profit in 2009, a significant milestone after years of focusing on growth showing improvement in profitability and revenue growth in 2010 which boosted investors confidence.Though it saw massive downfall in 2023, it’s again back on track.

Visualization 2: Trading Volume (Bar Chart)

b <- ggplot(stockdata, aes(x = Date, y = Volume)) +
   geom_bar(stat = "identity", fill = "#ff9900", alpha = 0.8) +
   labs(title = "Trading Volume Analysis",
        y = "Volume",
        x = "Date") +
   theme_minimal()
ggplotly(b)

##Explabation: It’s evident that Amazon used to sell more of its stock volumn than today but as the price are high, they are also high in valution now. # Visualization 3: Candlestick Chart

p <-plot_ly(stockdata, x = ~Date, type = "candlestick",
        open = ~Open, close = ~Close, high = ~High, low = ~Low) %>% 
  layout(title = "Candlestick Chart",
         xaxis = list(title = "Date"),
         yaxis = list(title = "Price"))
ggplotly(p)

##Explanation: The candlestick chart represents the open,high,low and close value of Amazon’s stock price. # Visualization 4: Animation

colnames(stockdata)[colnames(stockdata) == "Adj.Close"] <- "Price"

p <- ggplot(stockdata, aes(x = Date, y = Price, group = 1 )) +
  geom_line(color= "#ff9900", size= 1) +
  labs(title = "Stock Price Animation",
       x = "Date (Years)",
       y = "Price USD") +
   theme(
     plot.title = element_text(size = 18, face = "bold", color = "#146eb4"),
     panel.background = element_rect(fill = "#EDFFEC"),
    panel.grid.major = element_line(color = "lightblue", linetype = "dashed"),
    panel.grid.minor = element_line(color = "darkgreen", linetype = "dotted")
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
animation <- p + transition_reveal(Date)
animate(animation)
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

# Visualization 5: Candlechart Animation

p <- ggplot(stockdata, aes(x = Date)) +
  geom_segment(aes(xend = Date, y = Low, yend = High, color = Close < Open), linewidth = 1) +
  geom_point(aes(y = Open), color = "#146eb4", linewidth = 2) +
  geom_point(aes(y = Close), color = "#ff9900", linewidth = 2) +
  labs(title = "Monthly Candlestick Chart",
       x = "Date(Year)",
       y = "Price(USD)") +
  theme(
     plot.title = element_text(size = 18, face = "bold", color = "#146eb4"),
     panel.background = element_rect(fill = "beige"),
    panel.grid.major = element_line(color = "lightblue", linetype = "dashed"),
    panel.grid.minor = element_line(color = "darkgreen", linetype = "dotted")
  ) +
  scale_color_manual(values = c("TRUE" = "#ff9900", "FALSE" = "#146eb4")) +
  guides(color = FALSE)
## Warning in geom_point(aes(y = Open), color = "#146eb4", linewidth = 2):
## Ignoring unknown parameters: `linewidth`
## Warning in geom_point(aes(y = Close), color = "#ff9900", linewidth = 2):
## Ignoring unknown parameters: `linewidth`
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
animated_chart <- p + transition_states(Date, transition_length = 2, state_length = 1) +
  enter_fade() +
  exit_fade() +
  ease_aes('linear')
animated_chart