#First Three Layers are: Data, Aesthetics, 
#Geometrics. --- Basic Layers.
#Next Three Layers:Facets,Statistics,Coordinates
#Last Layer is: Theme Layer.
############
#install.packages('ggplot2')
#install.packages('ggplot2movies')
####### Examples##############
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.2
pl<-ggplot(data=mtcars,aes(x=mpg,y=hp))
## Add geometrics
pl+geom_point()

## Add facets
pl+geom_point()+facet_grid(cyl~.)

## Add statistics
pl+geom_point()+facet_grid(cyl~.)+stat_smooth()
## `geom_smooth()` using method = 'loess'

## Add coordinates
pl+geom_point()+facet_grid(cyl~.)+
  stat_smooth()+coord_cartesian(xlim=c(15,25))
## `geom_smooth()` using method = 'loess'

## Theme layer
pl+geom_point()+facet_grid(cyl~.)+
  stat_smooth()+
  coord_cartesian(xlim=c(15,25))+theme_dark()
## `geom_smooth()` using method = 'loess'

###########Histograms#########
#To work with Single numerical data
library(ggplot2movies)
## Warning: package 'ggplot2movies' was built under R version 3.3.2
##Movies dataset come with the above package
colnames(movies)
##  [1] "title"       "year"        "length"      "budget"      "rating"     
##  [6] "votes"       "r1"          "r2"          "r3"          "r4"         
## [11] "r5"          "r6"          "r7"          "r8"          "r9"         
## [16] "r10"         "mpaa"        "Action"      "Animation"   "Comedy"     
## [21] "Drama"       "Documentary" "Romance"     "Short"
pl<-ggplot(movies,aes(x=rating))
pl+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Changing binwidth
pl+geom_histogram(binwidth=.1)

#adding outer color,filling color and opacity
pl+geom_histogram(binwidth=.1,color='red',
                  fill='pink',
                  alpha=0.4)

#adding xlabel, ylabel and title to image.
pl+geom_histogram(binwidth=.1,color='red',
                  fill='pink',
                  alpha=0.4)+
  xlab('Movie Rating')+ylab('Frequency')+
  ggtitle("Distribution of User\'s Rating")

###########Scatter Plots #############
#Works with two numerical variables
df<-mtcars
pl<-ggplot(df,aes(x=wt,y=mpg))
pl+geom_point(size=2,alpha=.5)

#when we add aesthetics to geometry, it changes
#the appearance.
#We can change the apperance based on third
#variable.
#EX1:Here we control the shape and color based on 
#different cylinders and size is set to 5 commonly
#for all points.
pl+geom_point(aes(shape=factor(cyl),
                  color=factor(cyl)),
              size=5)

#EX2:
pl+geom_point(aes(color=hp),size=5)

pl+geom_point(aes(color=hp),size=5)+
  scale_color_gradient(low='blue',high='red')

########Bar plots ######
#mpg data set comes with ggplot2
colnames(mpg)
##  [1] "manufacturer" "model"        "displ"        "year"        
##  [5] "cyl"          "trans"        "drv"          "cty"         
##  [9] "hwy"          "fl"           "class"
#help(data=mpg)
help(mpg)
## starting httpd help server ...
##  done
###
df<-mpg
pl<-ggplot(df,aes(x=class))
pl+geom_bar(aes(fill=drv))

pl+geom_bar(aes(fill=drv),position="dodge")

#position:fill gives proportion details wrto others
pl+geom_bar(aes(fill=drv),position="fill")

###################
#### Box plots ########
#To depict groups of numerical data through
#their core file information.
df<-mtcars
pl<-ggplot(df,aes(x=factor(cyl),y=mpg))
pl+geom_boxplot()

pl+geom_boxplot(aes(fill=factor(cyl)))

pl+geom_boxplot(aes(fill=factor(cyl)))+
  theme_dark()

pl+geom_boxplot(aes(fill=factor(cyl)))+coord_flip()+
  theme_bw()

#####Two variable plotting#######
pl<-ggplot(movies,aes(x=year,y=rating))
pl+geom_bin2d()

pl+geom_bin2d()+
  scale_fill_gradient(high = 'red',low = 'green')

pl+geom_bin2d(binwidth=c(3,1))+
  scale_fill_gradient(high = 'red',low = 'green')

##install.packages('hexbin')
pl+geom_hex()+
  scale_fill_gradient(high = 'red',low = 'green')
## Warning: package 'hexbin' was built under R version 3.3.2

##density plots
pl+geom_density2d()

########Coordinates and faceting#######
pl<-ggplot(mpg,aes(x=displ,y=hwy))+geom_point()
pl

pl+coord_cartesian(xlim=c(1,4),ylim=c(15,30))

pl+coord_fixed(ratio = 1/3)

pl+coord_fixed(ratio=3/1)

pl+facet_grid(.~cyl)

pl+facet_grid(drv~.)

pl+facet_grid(drv~cyl)

#######Theme Layer########
#To set theme for all plots.
theme_set(theme_minimal())
#to set theme for specific plot
pl+theme_dark()

#to get more themes install
#install.packages('ggthemes')
library('ggthemes')
## Warning: package 'ggthemes' was built under R version 3.3.2
pl+theme_economist()

######plotly package################
#install.packages('plotly')
library(plotly)
## Warning: package 'plotly' was built under R version 3.3.2
## 
## 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
pl<-ggplot(mtcars,aes(mpg,wt))+geom_point()
ggplotly(pl)