# Data Visualization: ggplot2. Distribution plots
# Script for lesson #5
# Data Science Financial Club KNEU
# Lecturer: Roman Kornyliuk
# Date: 2017-06-27

# Source 1: http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/


# install.packages("tidyverse")
# install.packages("ggplot2")
# install.packages('mvtnorm')

library(tidyverse)
library(ggplot2)
library(mvtnorm)

# The seed number you choose is the starting point 
# used in the generation of a sequence of random numbers
set.seed(1234)
# create data frame
dat <- data.frame(sec = factor(rep(c("A","B"), each=200)), 
                  rating = c(rnorm(200), rnorm(200, mean=0.8) ))
# View first & last few rows
head(dat,15)
tail(dat)

dim(dat)

###----
# Histograms
###----

## Basic histogram from the vector "rating". Each bin is .5 wide.
## These both result in the same output:
ggplot(dat, aes(x=rating)) + 
        geom_histogram()

ggplot(dat, aes(x=rating)) + 
        geom_histogram(binwidth=.5)


# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
        geom_histogram(binwidth=.5, colour="black", fill="white")

###----
# Density plots
###----

# Density curve
ggplot(dat, aes(x=rating)) + 
        geom_density()

median(dat$rating)
mean(dat$rating)

# Histogram overlaid with kernel density curve
ggplot(dat, aes(x=rating)) + 
        geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                       binwidth=.5,
                       colour="black", fill="white") +
        geom_density(alpha=0.2, fill="#FF6666")  # Overlay with transparent density plot

# Add a line for the mean:
        
ggplot(dat, aes(x=rating)) +
        geom_histogram(binwidth=.5, colour="black", fill="white") +
        geom_vline( aes(xintercept=mean(rating, na.rm=T)),   # Ignore NA values for mean
                   color="red", linetype="dashed", size=1)

ggplot(dat, aes(x=rating)) +
        geom_histogram(binwidth=.5, colour="black", fill="white") +
        geom_vline( aes(xintercept=mean(rating, na.rm=T)),   # Ignore NA values for mean
                    color="blue", size=1)

###----
# Histograms for groups
###----

# Overlaid histograms
ggplot(dat, aes(x=rating, fill=sec)) + 
               geom_histogram(binwidth=.5, alpha=0.5, position="identity")

# Interleaved histograms
ggplot(dat, aes(x=rating, fill=sec)) +
        geom_histogram(binwidth=.5, position="dodge")


###----
# Density plots for groups
###----
# Density plots
ggplot(dat, aes(x=rating, colour=sec)) + 
        geom_density()

# Density plots with semi-transparent fill
ggplot(dat, aes(x=rating, fill=sec)) + 
        geom_density(alpha=.3)

# Find the mean of each group
library(plyr)
cdat <- ddply(dat, "sec", summarise, rating.mean=mean(rating))
cdat

# Overlaid histograms with means
ggplot(dat, aes(x=rating, fill=sec)) +
        geom_histogram(binwidth=.5, alpha=.5, position="identity") +
        geom_vline(data=cdat, aes(xintercept=rating.mean,  colour=sec),
                   linetype="dashed", size=1)

# Density plots with means
ggplot(dat, aes(x=rating, colour=sec)) +
        geom_density() +
        geom_vline(data=cdat, aes(xintercept=rating.mean,  colour=sec),
                   linetype="dashed", size=1)

# Using facets:
ggplot(dat, aes(x=rating)) + 
        geom_histogram(binwidth=.5, colour="black", fill="white") + 
        facet_grid(sec ~ .)

# With mean lines, using cdat from above
ggplot(dat, aes(x=rating)) + 
        geom_histogram(binwidth=.5, colour="black", fill="white") + 
        facet_grid(sec ~ .) +
        geom_vline(data=cdat, aes(xintercept=rating.mean),
                   linetype="dashed", size=1, colour="red")

###----
# Box plots
###----

# A basic box plot
ggplot(dat, aes(x=sec, y=rating)) + 
        geom_boxplot()

# A basic box with the secitions colored
ggplot(dat, aes(x=sec, y=rating, fill=sec)) + 
        geom_boxplot()

# The above adds a redundant legend. With the legend removed:
ggplot(dat, aes(x=sec, y=rating, fill=sec)) + 
        geom_boxplot() +
        guides(fill=FALSE)

# With flipped axes
ggplot(dat, aes(x=sec, y=rating, fill=sec)) + geom_boxplot() + 
        guides(fill=FALSE) + 
        coord_flip()

# It’s also possible to add the mean by using stat_summary.
# Add a diamond at the mean, and make it larger
ggplot(dat, aes(x=sec, y=rating)) + 
        geom_boxplot() +
        stat_summary(fun.y=mean, geom="point", shape=22, size=4)


###----
# 2D Histogram | Heatmap
###----

# Basic 2D Histogram
s <- matrix(c(1, -.75, -.75, 1), ncol = 2)
obs <- mvtnorm::rmvnorm(500, sigma = s)
p <- plot_ly(x = obs[,1], y = obs[,2])
pp <- subplot(
        p %>% add_markers(alpha = 0.2),
        p %>% add_histogram2d()
)
pp

# Colorscale
p <- p %>% add_histogram2d(colorscale = "Blues")
p

# Z Matrix
cnt <- with(diamonds, table(cut, clarity))
cnt
p <- plot_ly(diamonds, x = ~cut, y = ~clarity, z = ~cnt) %>%
        add_histogram2d()
p

