#install.packages("ggplot2", dependencies = TRUE)
#install.packages("gcookbook", dependencies = TRUE)
library(gcookbook)
?heightweight
## starting httpd help server ... done
class(heightweight)
## [1] "data.frame"
str(heightweight)
## 'data.frame':    236 obs. of  5 variables:
##  $ sex     : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ageYear : num  11.9 12.9 12.8 13.4 15.9 ...
##  $ ageMonth: int  143 155 153 161 191 171 185 142 160 140 ...
##  $ heightIn: num  56.3 62.3 63.3 59 62.5 62.5 59 56.5 62 53.8 ...
##  $ weightLb: num  85 105 108 92 112 ...
library(gcookbook)
library(ggplot2)
#plot(heightweight$height, heightweight$age)
ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()

ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(shape = 21)

ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point(shape=1.5)

ggplot(heightweight,aes(x=ageYear,y=heightIn,color=sex))+geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex)) + geom_point(size=3)+scale_shape_manual(values =c(1,4))

hw <- heightweight

hw$weightGroup <- cut(hw$weightLb, breaks=c(-Inf, 100, Inf), labels = c("<100", ">100"))
head(hw)
##   sex ageYear ageMonth heightIn weightLb weightGroup
## 1   f   11.92      143     56.3     85.0        <100
## 2   f   12.92      155     62.3    105.0        >100
## 3   f   12.75      153     63.3    108.0        >100
## 4   f   13.42      161     59.0     92.0        <100
## 5   f   15.92      191     62.5    112.5        >100
## 6   f   14.25      171     62.5    112.0        >100
class(hw$weightGroup)
## [1] "factor"
ggplot(hw, aes(x = ageYear, y = heightIn, shape=sex, fill=weightGroup)) + geom_point(size=2.5)+scale_shape_manual(values=c(21,24))+scale_fill_manual(values = c("red","black"),guide=guide_legend(override.aes = list(shape=21)))

ggplot(hw, aes(x = ageYear, y = heightIn, shape=sex, fill=weightGroup)) + geom_point(size=2.5)+scale_shape_manual(values=c(12,22))+scale_fill_manual(values = c("purple","gold"),guide=guide_legend(override.aes = list(shape=21)))

ggplot(heightweight, aes(x = ageYear, y= heightIn, color=weightLb)) + geom_point()

ggplot(heightweight, aes(x = ageYear, y= heightIn, size=weightLb)) + geom_point()

ggplot(heightweight, aes(x = weightLb, y = heightIn, fill=ageYear))+geom_point(shape = 21, size = 2.5)+scale_fill_gradient(low = "black", high="white")

ggplot(heightweight, aes(x=ageYear, y = heightIn, size = weightLb, color = sex))+geom_point(alpha=5)+scale_size_area(6)

#str(diamonds)

sp <- ggplot(diamonds, aes(x = carat, y = price))
sp + geom_point(alpha = .01)

str(ChickWeight)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   578 obs. of  4 variables:
##  $ weight: num  42 51 59 64 76 93 106 125 149 171 ...
##  $ Time  : num  0 2 4 6 8 10 12 14 16 18 ...
##  $ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15 15 ...
##  $ Diet  : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
##  - attr(*, "formula")=Class 'formula'  language weight ~ Time | Chick
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "outer")=Class 'formula'  language ~Diet
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ x: chr "Time"
##   ..$ y: chr "Body weight"
##  - attr(*, "units")=List of 2
##   ..$ x: chr "(days)"
##   ..$ y: chr "(gm)"
sp1 <- ggplot(ChickWeight, aes(x = Time, y = weight))
sp1 + geom_point()

sp1 + geom_point(position = "jitter")

sp1 + geom_point(position = position_jitter(width=.5,height = 0))

sp1 <- ggplot(ChickWeight, aes(x = Time, y = weight))
sp1 + geom_boxplot()
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

sp1 + geom_boxplot(aes(group=Time))

sp + geom_point() + stat_smooth(method=lm)
## `geom_smooth()` using formula = 'y ~ x'

sp + geom_point() + stat_smooth(method=lm, level=.99)
## `geom_smooth()` using formula = 'y ~ x'

sp + geom_point()+ stat_smooth(method = lm, se= FALSE)
## `geom_smooth()` using formula = 'y ~ x'

sp + geom_point(color="blue")+ stat_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

library(MASS)

b <- biopsy

b$classn[b$class == "benign"] <- 0
b$classn[b$class== "malignant"] <- 1
ggplot(b, aes(x = V1, y = classn)) + geom_point(position = position_jitter(width = .3, height = .06), alpha = .4, shape = 21, size = 1.5)+stat_smooth(method = "glm", method.args = list(family = "binomial"))
## `geom_smooth()` using formula = 'y ~ x'

sps <- ggplot(heightweight, aes (x = ageYear, y = heightIn, color = sex))+geom_point()+scale_color_brewer(palette = "Set1")

sps + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

sps + geom_smooth(method = lm, se = FALSE, fullrange = TRUE)
## `geom_smooth()` using formula = 'y ~ x'

#Lines

ggplot(BOD, aes(x = Time, y = demand)) + geom_line()

BOD1 <- BOD
BOD1$Time <- factor(BOD1$Time)

BOD1$Time <-factor(BOD1$Time)

ggplot(BOD1, aes(x =Time, y =demand, group =1))+geom_line()

ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + ylim(0, max(BOD$demand))

ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + expand_limits(y =0)