ls()
## character(0)
rm(list=ls())
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 292299  7.9     592000 15.9   460000 12.3
## Vcells 330854  2.6     786432  6.0   677388  5.2
#http://rgraphgallery.blogspot.in/


X = seq(1, 100, 5)
Y = seq (1, 100, 5)
Z = rnorm (length (X), 10, 2)
data1 <- data.frame (X, Y,Z)
data2 <- data.frame (X, Y, Z1 = Z - 5)
data3 <- data.frame (X, Y, Z1 = Z - 3)

require(scatterplot3d)
## Loading required package: scatterplot3d
s3d <- scatterplot3d(data1, color = "blue", pch = 19, xlim=NULL, ylim=NULL, zlim= c(0, 20))
s3d$points3d(data2, col = "red", pch = 18)
s3d$points3d(data3, col = "green4", pch = 17)

#graph2

myd <- data.frame(   var1  = c(1,1,2,2,3,3,4,4),   
                     samp = c("A","B","A","B","A","B","A","B"),   
                     Value1 = c(3.5,2,5,8,3,2,7,2), Value2 = c(1.5,0,5,5,3,0,4,5) ) 
# rshaping data to long form for ggplot2 
library(reshape2)
meltd<- melt(myd, id.vars=1:2) 

#plot 
library(ggplot2)

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_bar(stat="identity") + facet_grid(~samp) + theme_bw()

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_bar(stat="identity") + facet_grid(~samp) 

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_bar(stat="identity") 

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_density(stat="identity") 

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_density(stat="identity")+
facet_grid(~samp)

ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
  geom_density(stat="identity")+
  facet_grid(~samp)+ theme_classic()

ggplot(myd, aes(x=var1, y=Value1, fill=samp)) +
  geom_density(stat="identity")+
  facet_grid(~samp)+ theme_classic()

#Graph3
#Source http://www.wekaleamstudios.co.uk/posts/displaying-data-using-level-plots/
#install.packages("geoR")

require(geoR)
## Loading required package: geoR
## --------------------------------------------------------------
##  Analysis of Geostatistical Data
##  For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
##  geoR version 1.7-5.1 (built on 2015-04-15) is now loaded
## --------------------------------------------------------------
data(elevation)
elevation.df = data.frame(x = 50 * elevation$coords[,"x"],
                          y = 50 * elevation$coords[,"y"], z = 10 * elevation$data)

elevation.loess = loess(z ~ x*y, data = elevation.df,
                        degree = 2, span = 0.25)

elevation.fit = expand.grid(list(x = seq(10, 300, 1), y = seq(10, 300, 1)))

z = predict(elevation.loess, newdata = elevation.fit)

elevation.fit$Height = as.numeric(z)

image(seq(10, 300, 1), seq(10, 300, 1), z,
      xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
      main = "Surface elevation data")
box()

library(lattice)
levelplot(Height ~ x*y, data = elevation.fit,
          xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
          main = "Surface elevation data",
          col.regions = terrain.colors(100)
)

levelplot(Height ~ x*y, data = elevation.fit,
          xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
          main = "Surface elevation data",
          col.regions = heat.colors(100)
)

levelplot(Height ~ x*y, data = elevation.fit,
          xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
          main = "Surface elevation data",
          col.regions = rainbow(100,0.5)
)

require(grDevices) # for colours
filled.contour(volcano, color = terrain.colors, asp = 1) # simple

x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
filled.contour(x, y, volcano, color = terrain.colors,
               plot.title = title(main = "The Topography of Maunga Whau",
                                  xlab = "Meters North", ylab = "Meters West"),
               plot.axes = { axis(1, seq(100, 800, by = 100))
                 axis(2, seq(100, 600, by = 100)) },
               key.title = title(main = "Height\n(meters)"),
               key.axes = axis(4, seq(90, 190, by = 10)))  # maybe also asp = 1
mtext(paste("filled.contour(.) from", R.version.string),
      side = 1, line = 4, adj = 1, cex = .66)

#http://docs.ggplot2.org/0.9.3.1/stat_contour.html

#barplot

barplot(table(iris$Species,iris$Sepal.Length))

barplot(table(iris$Species,iris$Sepal.Length),col=heat.colors(5,0.6))

#use readxl package
#getwd()
#setwd("C:/Users/dell/Desktop")
#use exact file name 

#rug plot
hist(iris$Sepal.Length,breaks=10)
rug(iris$Sepal.Length)

getwd()
## [1] "C:/Users/dell"
setwd("C:/Users/dell/Desktop")
library(data.table)

bigdiamonds=fread("BigDiamonds.csv")
## 
Read 23.4% of 598024 rows
Read 50.2% of 598024 rows
Read 75.2% of 598024 rows
Read 598024 rows and 13 (of 13) columns from 0.049 GB file in 00:00:06
hist(bigdiamonds$carat,breaks=100)

#rug(diamonds$carat)

barplot(bigdiamonds[,mean(carat),color]$V1)

par(mfrow=c(2,1))
par(bg="yellow")

#boxplot(iris$Sepal.Length~iris$Species,xlim=c(0.1,2.5),ylim=c(3,10))
boxplot(iris$Sepal.Length~iris$Species,xlim=c(0.1,2.5))


#data viz using RColorBrewer

library(RColorBrewer)
#par(mfrow=c(2,2))
# barplot(table(iris$Species,iris$Sepal.Length),col=brewer.pal(3,"Set1")
# barplot(table(iris$Species,iris$Sepal.Length),col=brewer.pal(3,"Set2"))
# barplot(table(iris$Species,iris$Sepal.Length),col=brewer.pal(3,"Set3"))
# barplot(table(iris$Species,iris$Sepal.Length),col=brewer.pal(3,"Blues"))

library(tabplot)
## Loading required package: ffbase
## Loading required package: ff
## Loading required package: bit
## Attaching package bit
## package:bit (c) 2008-2012 Jens Oehlschlaegel (GPL-2)
## creators: bit bitwhich
## coercion: as.logical as.integer as.bit as.bitwhich which
## operator: ! & | xor != ==
## querying: print length any all min max range sum summary
## bit access: length<- [ [<- [[ [[<-
## for more help type ?bit
## 
## Attaching package: 'bit'
## 
## The following object is masked from 'package:data.table':
## 
##     setattr
## 
## The following object is masked from 'package:base':
## 
##     xor
## 
## Attaching package ff
## - getOption("fftempdir")=="C:/Users/dell/AppData/Local/Temp/RtmpQ1lgyi"
## 
## - getOption("ffextension")=="ff"
## 
## - getOption("ffdrop")==TRUE
## 
## - getOption("fffinonexit")==TRUE
## 
## - getOption("ffpagesize")==65536
## 
## - getOption("ffcaching")=="mmnoflush"  -- consider "ffeachflush" if your system stalls on large writes
## 
## - getOption("ffbatchbytes")==16095641.6 -- consider a different value for tuning your system
## 
## - getOption("ffmaxbytes")==804782080 -- consider a different value for tuning your system
## 
## 
## Attaching package: 'ff'
## 
## The following objects are masked from 'package:bit':
## 
##     clone, clone.default, clone.list
## 
## The following objects are masked from 'package:utils':
## 
##     write.csv, write.csv2
## 
## The following objects are masked from 'package:base':
## 
##     is.factor, is.ordered
## 
## 
## Attaching package: 'ffbase'
## 
## The following objects are masked from 'package:ff':
## 
##     [.ff, [<-.ff
## 
## The following objects are masked from 'package:base':
## 
##     %in%, table
str(diamonds)
## 'data.frame':    53940 obs. of  10 variables:
##  $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##  $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##  $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##  $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
library(ggplot2)
data(diamonds)
str(diamonds)
## 'data.frame':    53940 obs. of  10 variables:
##  $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##  $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##  $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##  $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
##  $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
##  $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
tableplot(diamonds)

diamonds2=na.omit(bigdiamonds)
ls()
##  [1] "bigdiamonds"     "data1"           "data2"          
##  [4] "data3"           "diamonds"        "diamonds2"      
##  [7] "elevation"       "elevation.df"    "elevation.fit"  
## [10] "elevation.loess" "meltd"           "myd"            
## [13] "s3d"             "x"               "X"              
## [16] "y"               "Y"               "z"              
## [19] "Z"
str(diamonds2)
## Classes 'data.table' and 'data.frame':   593784 obs. of  13 variables:
##  $ V1          : chr  "494" "495" "496" "497" ...
##  $ carat       : num  0.24 0.31 0.26 0.24 0.3 0.34 0.2 0.29 0.22 0.25 ...
##  $ cut         : chr  "V.Good" "V.Good" "Good" "Ideal" ...
##  $ color       : chr  "G" "K" "J" "G" ...
##  $ clarity     : chr  "SI1" "SI2" "VS2" "SI1" ...
##  $ table       : num  61 59 56.5 55 57 66 62 58 62 64 ...
##  $ depth       : num  58.9 60.2 64.1 61.3 62.2 55 59.1 61.4 59.6 60.5 ...
##  $ cert        : chr  "GIA" "GIA" "IGI" "GIA" ...
##  $ measurements: chr  "4.09 x 4.10 x 2.41" "4.40 x 4.42 x 2.65" "4.01 x 4.05 x 2.58" "4.01 x 4.03 x 2.47" ...
##  $ price       : int  300 300 300 300 300 300 301 301 301 301 ...
##  $ x           : num  4.09 4.4 4.01 4.01 4.21 4.75 3.79 4.25 3.9 4.02 ...
##  $ y           : num  4.1 4.42 4.05 4.03 4.24 4.61 3.82 4.31 3.93 4.06 ...
##  $ z           : num  2.41 2.65 2.58 2.47 2.63 2.57 2.25 2.63 2.33 2.44 ...
##  - attr(*, ".internal.selfref")=<externalptr>
setkey(diamonds2,carat)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:data.table':
## 
##     between, last
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
diamonds3=select(diamonds2,price,carat,cut,color,clarity,x,y,z)

diamonds3$cut=as.factor(diamonds3$cut)
diamonds3$color=as.factor(diamonds3$color)
diamonds3$clarity=as.factor(diamonds3$clarity)

str(diamonds3)
## Classes 'data.table' and 'data.frame':   593784 obs. of  8 variables:
##  $ price  : int  301 301 303 304 304 306 307 308 308 308 ...
##  $ carat  : num  0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ...
##  $ cut    : Factor w/ 3 levels "Good","Ideal",..: 3 3 2 2 2 2 3 2 3 2 ...
##  $ color  : Factor w/ 9 levels "D","E","F","G",..: 6 2 2 3 4 4 3 7 6 4 ...
##  $ clarity: Factor w/ 9 levels "I1","I2","IF",..: 9 9 7 6 6 6 8 7 9 7 ...
##  $ x      : num  3.79 3.72 3.75 3.82 2.27 3.79 3.74 3.73 3.74 3.81 ...
##  $ y      : num  3.82 3.75 3.76 3.84 3.78 3.81 3.77 3.75 3.75 3.83 ...
##  $ z      : num  2.25 2.35 2.35 2.33 3.82 2.32 2.32 2.32 2.31 2.28 ...
##  - attr(*, "sorted")= chr "carat"
##  - attr(*, ".internal.selfref")=<externalptr>
tableplot(diamonds3)