library(ggplot2)


library(gcookbook)
library(plyr)
library(MASS)

ggplot(birthwt, aes(x=bwt)) + geom_histogram(fill= "white", colour = "black") + facet_grid(smoke ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

str(birthwt)
## 'data.frame':    189 obs. of  10 variables:
##  $ low  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
##  $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
##  $ race : int  2 3 1 1 1 3 1 3 1 1 ...
##  $ smoke: int  0 0 1 1 1 0 0 0 1 1 ...
##  $ ptl  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ht   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ui   : int  1 0 0 1 1 0 0 0 0 0 ...
##  $ ftv  : int  0 3 1 2 0 0 1 1 1 0 ...
##  $ bwt  : int  2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 ...
birthwt1 <- birthwt
birthwt1$smoke <- factor(birthwt1$smoke)

library(plyr)
birthwt1$smoke <- revalue(birthwt1$smoke, c("0" = "No Smoke", "1" = "Smoke"))

ggplot(birthwt1, aes(x=bwt)) + geom_histogram(fill="white", colour = "black") + facet_grid(smoke ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(birthwt, aes(x=bwt)) + geom_histogram(fill = "white", colour = "black") + facet_grid(race ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(birthwt, aes(x=bwt)) + geom_histogram(fill = "white", colour = "black") + facet_grid(race ~ ., scales = "free")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

birthwt1smoke <- factor(birthwt1$smoke)

ggplot(birthwt1, aes(x=bwt, fill=birthwt1smoke)) + geom_histogram (binwidth = 500, position = "identity", alpha=0.4)

summary(birthwt1smoke)
## No Smoke    Smoke 
##      115       74
str(birthwt1)
## 'data.frame':    189 obs. of  10 variables:
##  $ low  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
##  $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
##  $ race : int  2 3 1 1 1 3 1 3 1 1 ...
##  $ smoke: Factor w/ 2 levels "No Smoke","Smoke": 1 1 2 2 2 1 1 1 2 2 ...
##  $ ptl  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ht   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ui   : int  1 0 0 1 1 0 0 0 0 0 ...
##  $ ftv  : int  0 3 1 2 0 0 1 1 1 0 ...
##  $ bwt  : int  2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 ...
ggplot(birthwt1, aes(x=bwt, colour= birthwt1smoke)) + geom_density()

ggplot(birthwt1, aes(x=bwt, fill= birthwt1smoke)) + geom_density (alpha=.3)

ggplot(birthwt1, aes(x=bwt, fill=birthwt1smoke)) + geom_histogram (aes(y = ..density..),binwidth = 500, position = "identity", alpha=0.4) + geom_density(alpha = .3)

ggplot(birthwt1, aes(x=bwt, fill=birthwt1smoke)) + geom_histogram(binwidth=500,position = "identity", alpha=0.4) + geom_vline(xintercept = 2500, colour = "red", linetype = "dashed")

ggplot(birthwt1, aes(x=bwt, fill=birthwt1smoke)) + geom_histogram(binwidth=500,position = "identity", alpha=0.4) + geom_vline(xintercept = 2500, colour = "red", linetype = "dashed") + ylab("Frequency") + xlab("Birth Weight (grams)") +
        labs(title = "Birth Weight of Babies Born to Smokers and Non-Smokers", caption = "Babies under 2500 grams are considered low weight, represented by red dotted line.")+ guides(fill=guide_legend(title=NULL))+ annotate("text", x=1500, y=35, label= "low weight", colour = "red")

ggplot(birthwt1, aes(x=bwt, fill= birthwt1smoke)) + geom_density (alpha=.3) + geom_vline(xintercept = 2500, colour = "red", linetype = "dashed") + ylab("Density") + xlab("Birth Weight (grams)") +
        labs(title = "Birth Weight of Babies Born to Smokers and Non-Smokers", caption = "Babies under 2500 grams are considered low weight, represented by red dotted line.")+ guides(fill=guide_legend(title=NULL))+ annotate("text", x=1500, y=5e-4, label= "low weight", colour = "red")