# Data Visualization. Plotly: introduction
# Script for lesson #5
# Data Science Financial Club KNEU
# Lecturer: Roman Kornyliuk
# Date: 2017-06-20

# Source 1: https://plot.ly/r/getting-started/ 
# Source 2: https://plot.ly/r/ 


# install.packages("tidyverse")
# install.packages("plotly")                    # 1st variant
# devtools::install_github("ropensci/plotly")   # 2nd variant

library(tidyverse)
library(plotly)

# Boxplot
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p

?midwest

# Scatterplot
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat,
        size = ~carat, text = ~paste("Clarity: ", clarity))

# ggplot2 Integration:
p <- ggplot(data = d, aes(x = carat, y = price)) +
        geom_point(aes(text = paste("Clarity:", clarity))) +
        geom_smooth(aes(colour = cut, fill = cut)) + 
        facet_wrap(~ cut)

ggplotly(p)

# Histogram
packageVersion('plotly')
p <- plot_ly(x = ~rnorm(50), type = "histogram")
p

# Overlaid histograms
p <- plot_ly(alpha = 0.6) %>%
        add_histogram(x = ~rnorm(500)) %>%
        add_histogram(x = ~rnorm(500) + 1) %>%
        layout(barmode = "overlay")
p

# Basic Boxplot
p <- plot_ly(y = ~rnorm(50), type = "box") %>%
        add_trace(y = ~rnorm(50, 1))
p

# Adding Jittered Points
p <- plot_ly(y = ~rnorm(50), type = "box", boxpoints = "all", jitter = 0.3,
             pointpos = -1.8)
p

# Several Box Plots
p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")
p

# Grouped Box Plots
p <- plot_ly(ggplot2::diamonds, x = ~cut, y = ~price, color = ~clarity, type = "box") %>%
        layout(boxmode = "group")

# Styling Outliers
y1 <- c(0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15,
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25)
y2 <- c(0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15,
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25)
y3 <- c(0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15,
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25)
y4 <- c(0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15,
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25)

p <- plot_ly(type = 'box') %>%
        add_boxplot(y = y1, jitter = 0.3, pointpos = -1.8, boxpoints = 'all',
                    marker = list(color = 'rgb(7,40,89)'),
                    line = list(color = 'rgb(7,40,89)'),
                    name = "All Points") %>%
        add_boxplot(y = y2, name = "Only Whiskers", boxpoints = FALSE,
                    marker = list(color = 'rgb(9,56,125)'),
                    line = list(color = 'rgb(9,56,125)')) %>%
        add_boxplot(y = y3, name = "Suspected Outlier", boxpoints = 'suspectedoutliers',
                    marker = list(color = 'rgb(8,81,156)',
                                  outliercolor = 'rgba(219, 64, 82, 0.6)',
                                  line = list(outliercolor = 'rgba(219, 64, 82, 1.0)',
                                              outlierwidth = 2)),
                    line = list(color = 'rgb(8,81,156)')) %>%
        add_boxplot(y = y4, name = "Whiskers and Outliers", boxpoints = 'outliers',
                    marker = list(color = 'rgb(107,174,214)'),
                    line = list(color = 'rgb(107,174,214)')) %>%
        layout(title = "Box Plot Styling Outliers")
