Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

# Load necessary libraries

library(ggplot2)
library(reshape2)
library(plotly)

# Create a data frame with the provided data


market_data <- data.frame(
Date = as.Date(c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-
01-05")),
Stock_Price = c(100, 102, 98, 105, 108),
Volume_Traded = c(2.5, 3.0, 2.2, 2.8, 3.5),
Market_Cap = c(500, 510, 490, 525, 540)
)

# 1. How does stock price relate to both volume traded and market capitalization?
print(market_data)

# 2. Create a 3D scatter plot to visualize the relationship between volume traded,


market cap, and stock price
plot_ly(market_data, x = ~Volume_Traded, y = ~Market_Cap, z = ~Stock_Price, type =
'scatter3d', mode = 'markers') %>%
layout(title = "3D Scatter Plot of Volume Traded, Market Cap, and Stock Price",
scene = list(xaxis = list(title = "Volume Traded (millions)"),
yaxis = list(title = "Market Cap ($ millions)"),
zaxis = list(title = "Stock Price ($)")))

# 3. Identify any clustering or outliers in the 3D plot


# The plot created above can be used to visually inspect for clusters or outliers.

# 4. Generate a 3D surface plot to illustrate how market capitalization changes


with variations in stock price and volume traded
market_grid <- expand.grid(
Stock_Price = seq(min(market_data$Stock_Price), max(market_data$Stock_Price),
length.out = 10),
Volume_Traded = seq(min(market_data$Volume_Traded),
max(market_data$Volume_Traded), length.out = 10)
)

market_grid$Market_Cap <- with(market_data,


approx(x = Stock_Price, y = Market_Cap, xout =
market_grid$Stock_Price)$y +
approx(x = Volume_Traded, y = Market_Cap, xout =
market_grid$Volume_Traded)$y) / 2

plot_ly(market_grid, x = ~Stock_Price, y = ~Volume_Traded, z = ~Market_Cap, type =


'surface') %>%
layout(title = "3D Surface Plot of Market Cap vs Stock Price and Volume Traded",
scene = list(xaxis = list(title = "Stock Price ($)"),
yaxis = list(title = "Volume Traded (millions)"),
zaxis = list(title = "Market Cap ($ millions)")))

# 5. Compare the 3D plots of stock price against both volume traded and market cap
separately
# 3D plot of Stock Price vs Volume Traded
plot_ly(market_data, x = ~Volume_Traded, y = ~Stock_Price, z = ~Market_Cap, type =
'scatter3d', mode = 'markers') %>%
layout(title = "3D Scatter Plot of Stock Price vs Volume Traded",
scene = list(xaxis = list(title = "Volume Traded (millions)"),
yaxis = list(title = "Stock Price ($)"),
zaxis = list(title = "Market Cap ($ millions)")))

# 3D plot of Stock Price vs Market Cap


plot_ly(market_data, x = ~Market_Cap, y = ~Stock_Price, z = ~Volume_Traded, type =
'scatter3d', mode = 'markers') %>%
layout(title = "3D Scatter Plot of Stock Price vs Market Cap",
scene = list(xaxis = list(title = "Market Cap ($ millions)"),
yaxis = list(title = "Stock Price ($)"),
zaxis = list(title = "Volume Traded (millions)")))

# Save plots to file if needed


# ggsave("volume_vs_market_cap_stock_price.png")
# saveWidget(plot_ly, "3d_scatter_plot.html")
# saveWidget(plot_ly, "3d_surface_plot.html")

You might also like