Author name: Syed Nabil Ahsan

In this project, the author sought to experiment different types of data sets through unique visualization methods.

#Global Plastic Production

data <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/global-plastics-production.csv")

library("ggplot2")

ggplot(data = data) +
  geom_point(mapping = aes(x = Global.plastics.production..million.tonnes., y = Year, alpha = Global.plastics.production..million.tonnes.), col = "Orange") +
  labs(x = "Metric Tonnes", y = "Year", title = "Global Plastic Pollution")

#Bangladesh Crime Statistics

data <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/Crime Statistics Of Bangladesh 2010-2019.csv")

 
par(mfrow=c(1,5))

plot(data$Dacoity)
plot(data$Robbery)
plot(data$Explosive)
plot(data$Narcotics)
plot(data$Smuggling)

library(ggplot2)
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
data <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/Crime Statistics Of Bangladesh 2010-2019.csv")


ggplot(data = data, aes(x = Year , y = Total.Cases, text = paste("Year : ", Year, "<br>Total.Cases : ", Total.Cases ))) +
  geom_point(col = "firebrick", alpha = 0.4) +
  
  labs(x = "Year",
       y = "Total Crime",
       title = "Total Cases Over Time") +
  
  
  theme_minimal() + 
  
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 12, face = "bold"),
        plot.title = element_text(size = 14, face = "bold"))

c <- ggplot(data = data, aes(x=Year, y = Total.Cases))


c + geom_boxplot(aes(col= Total.Cases))
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
## Warning: The following aesthetics were dropped during statistical transformation: colour
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

c + 
  geom_boxplot(outlier.shape = NA, fill = "grey", color = "darkblue", alpha = 0.5) + 
  geom_jitter(width = 0.3, alpha = 0.3, color = "firebrick") +
  
  labs(y = "Total Cases",
       title = "Distribution of MFST Prices",
       subtitle = "Boxplot with Jittered Points",
       caption = "Source: Your Data Source") +
  theme_minimal() +
  theme(plot.title = element_text(size = 16, face = "bold"),
        axis.title = element_text(size = 14, face = "bold"),
        axis.text = element_text(size = 12),
        legend.position = "bottom")
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

#Microsoft Stock Analysis

MFTS <-read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/Microsoft Historical Data.csv")

par(mfrow=c(1,2))
plot(MFTS$Price)
hist(MFTS$Price, breaks=200, col='Yellow')

library(ggplot2)
library(plotly)

MFTS <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/Microsoft Historical Data.csv")

head(MFTS)
##        Date Price  Open  High   Low    Vol. Change..
## 1 1/23/2004 28.48 28.27 28.76 28.22 127.27M    1.68%
## 2 1/26/2004 28.80 28.49 28.83 28.32  58.32M    1.12%
## 3 1/27/2004 28.25 28.60 28.72 28.22  63.23M   -1.91%
## 4 1/28/2004 27.71 28.28 28.44 27.47  71.25M   -1.91%
## 5 1/29/2004 27.91 27.78 27.95 27.57  63.75M    0.72%
## 6 1/30/2004 27.65 27.85 27.90 27.55  40.53M   -0.93%
summary(MFTS)
##      Date               Price             Open             High       
##  Length:4993        Min.   : 15.15   Min.   : 15.20   Min.   : 15.62  
##  Class :character   1st Qu.: 27.28   1st Qu.: 27.28   1st Qu.: 27.49  
##  Mode  :character   Median : 37.06   Median : 36.97   Median : 37.40  
##                     Mean   : 87.27   Mean   : 87.25   Mean   : 88.12  
##                     3rd Qu.:108.59   3rd Qu.:108.90   3rd Qu.:109.69  
##                     Max.   :377.44   Max.   :373.46   Max.   :378.87  
##       Low             Vol.             Change..        
##  Min.   : 14.87   Length:4993        Length:4993       
##  1st Qu.: 27.07   Class :character   Class :character  
##  Median : 36.62   Mode  :character   Mode  :character  
##  Mean   : 86.36                                        
##  3rd Qu.:107.60                                        
##  Max.   :371.25
par(mfrow = c(1, 2))
plot(MFTS$Price)
hist(MFTS$Price, breaks = 200, col = 'Yellow')

ggplot(data = MFTS, aes(x = Date, y = Price, text = paste("Date: ", Date, "<br>Price: ", Price))) +
  geom_point(col = "orange", alpha = 0.35) +

  labs(x = "Date",
       y = "Price",
       title = "MFTS Stock Price Trend Over Time") +
  
 
theme_minimal() + 

  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 12, face = "bold"),
        plot.title = element_text(size = 14, face = "bold"))

c <- ggplot(data = MFTS, aes(x="Days", y = Price))

c + geom_boxplot()

c + 
  geom_boxplot(outlier.shape = NA, fill = "grey", color = "darkblue", alpha = 0.5) + 
  geom_jitter(width = 0.3, alpha = 0.3, color = "orange") +
  
  labs(y = "Price",
       title = "Distribution of MFST Prices",
       subtitle = "Boxplot with Jittered Points",
       caption = "Source: Your Data Source") +
  theme_minimal() +
  theme(plot.title = element_text(size = 16, face = "bold"),
        axis.title = element_text(size = 14, face = "bold"),
        axis.text = element_text(size = 12),
        legend.position = "bottom")

library(ggplot2)
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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.4     ✔ stringr 1.5.1
## ✔ forcats 1.0.0     ✔ tibble  3.2.1
## ✔ purrr   1.0.2     ✔ tidyr   1.3.0
## ✔ readr   2.1.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)

Microsoft<- read.csv("/Users/nabil/Downloads/Microsoft - Sheet1.csv")

stock_data <- data.frame(Date = Microsoft$Date, Price = Microsoft$Price)

library(forecast)
stock_ts <- ts(Microsoft$Price, frequency = 365)
decomposition <- decompose(stock_ts)
plot(decomposition)

library(forecast)
stock_ts <- ts(Microsoft$Price, frequency = 365)
decomposition <- decompose(stock_ts)
plot(decomposition)

decomposition <- decompose(stock_ts)

trend <- decomposition$trend
seasonal <- decomposition$seasonal
residual <- decomposition$random

decomposition_df <- data.frame(Date = time(stock_ts), Original = as.vector(stock_ts),
                               Trend = trend, Seasonal = seasonal, Residual = residual)


library(ggplot2)
p <- ggplot(decomposition_df, aes(x = Date)) +
  geom_line(aes(y = Original), color = "orange") +
  geom_line(aes(y = Trend), color = "firebrick") +
  geom_line(aes(y = Seasonal), color = "steelblue") +
  geom_line(aes(y = Residual), color = "magenta") +
  labs(title = "Time Series Decomposition",
       subtitle = "Microsoft") +
  theme_minimal()

p
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
## Warning: Removed 364 rows containing missing values (`geom_line()`).
## Removed 364 rows containing missing values (`geom_line()`).

library(ggplot2)
library(gganimate)

stock_data$Date <- as.Date(Microsoft$Date, format = "%m/%d/%y")

z <- ggplot(stock_data, aes(x = Date, y = Price, group = 1)) +
  geom_point(color = "firebrick", size = 3) +
  labs(title = "MFTS Stock Price Animation",
       x = "Date",
       y = "Price") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 18, face = "bold", color = "black"),
    panel.background = element_rect(fill = "white"),
    panel.grid.major = element_line(color = "grey", linetype = "dashed"),
    panel.grid.minor = element_line(color = "grey", linetype = "dotted")
  )

anim <- z +
  transition_states(Date, transition_length = 2, state_length = 0.05) +
  enter_fade() +
  exit_fade() +
  ease_aes('linear')

anim +
  theme_void() +
  enter_fade() +
  exit_fade()

anim

#Square Pharmaceuticals Stock Price Analysis

SQPH <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/square  - SQPH Historical Data.csv")

sqph_data <- data.frame(Date = SQPH$Date, Price = SQPH$Price)


library(forecast)
library(gganimate)
stock_s <- ts(SQPH$Price, frequency = 250)
decomposition <- decompose(stock_s)
plot(decomposition)

SQPH <- read.csv("/Users/nabil/Desktop/Essentials/Data Visualisation with R/square  - SQPH Historical Data.csv")

sqph_data <- data.frame(Date = SQPH$Date, Price = SQPH$Price)

library(forecast)
library(gganimate)
stock_s <- ts(SQPH$Price, frequency = 250)
decomposition <- decompose(stock_s)


trend <- decomposition$trend
seasonal <- decomposition$seasonal
residual <- decomposition$random

decomposition_df <- data.frame(Date = time(stock_s), Original = as.vector(stock_s),
                               Trend = trend, Seasonal = seasonal, Residual = residual)


library(ggplot2)
g <- ggplot(decomposition_df, aes(x = Date)) +
  geom_line(aes(y = Original), color = "orange") +
  geom_line(aes(y = Trend), color = "firebrick") +
  geom_line(aes(y = Seasonal), color = "steelblue") +
  geom_line(aes(y = Residual), color = "magenta") +
  labs(title = "Time Series Decomposition",
       subtitle = "Square Pharmaceuticals") +
  theme_minimal()

g
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
## Warning: Removed 250 rows containing missing values (`geom_line()`).
## Removed 250 rows containing missing values (`geom_line()`).

library(ggplot2)
library(gganimate)


sqph_data$Date <- as.Date(sqph_data$Date, format = "%m/%d/%y")
 
p <- ggplot(sqph_data, aes(x = Date, y = Price, group = 1)) +
  geom_point(color = "firebrick", size = 1.5) +
  labs(title = "SQPH Stock Price Animation",
       x = "Date",
       y = "Price") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 18, face = "bold", color = "black"),
    panel.background = element_rect(fill = "white"),
    panel.grid.major = element_line(color = "grey", linetype = "dashed"),
    panel.grid.minor = element_line(color = "grey", linetype = "dotted")
  )

anim <- p +
  transition_states(Date, transition_length = 2, state_length = 0.05) +
  enter_fade() +
  exit_fade() +
  ease_aes('linear')

anim +
  theme_void() +
  enter_fade() +
  exit_fade()

anim

#GDP per Capita

library(ggplot2)
library(gganimate)
library(gapminder)


gapplot<-ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')

gapplot