library(ggplot2)
View(ChickWeight)
ggplot(ChickWeight, aes(x=Time, y=weight))+geom_boxplot(aes(group=Time))
# Grouping response variable(Weight) acc. to time.
ggplot(ChickWeight, aes(x=Time, y=weight))+geom_boxplot(aes(group=Time))+facet_grid(.~Diet)
# Charting and graphing discrete data.
data("diamonds")
ggplot(diamonds, aes(x=cut))+geom_bar()
# For cluster graphs/stacked bars.
ggplot(diamonds, aes(clarity, fill=cut))+geom_bar(position = "dodge")
# For seperate panels.
ggplot(diamonds, aes(cut, fill=cut))+geom_bar()+facet_grid(~clarity)
# Deriving insights from qualitative/nominal data. ## Finding whether or not their is association b/w gender & food.
men <- c(189,142,90,23)
women <- c(50,99,189,70)
MeWo <- cbind.data.frame(men,women)
food_servey <- as.data.frame(MeWo)
chisq.test(food_servey)
##
## Pearson's Chi-squared test
##
## data: food_servey
## X-squared = 146.13, df = 3, p-value < 2.2e-16
p-Value is < 0.05. so, I would say their is no assocaition b/w gender & food. # Frequency Tables.
library(MASS)
data(survey)
levels(survey$Smoke)
## [1] "Heavy" "Never" "Occas" "Regul"
St <- table(survey$Smoke)
St
##
## Heavy Never Occas Regul
## 11 189 19 17
ct <- xtabs(~Sex+Smoke, data = survey)
ftable(ct)
## Smoke Heavy Never Occas Regul
## Sex
## Female 5 99 9 5
## Male 6 89 10 12
## OR
library(gmodels)
CrossTable(survey$Sex, survey$Smoke, prop.chisq = FALSE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 235
##
##
## | survey$Smoke
## survey$Sex | Heavy | Never | Occas | Regul | Row Total |
## -------------|-----------|-----------|-----------|-----------|-----------|
## Female | 5 | 99 | 9 | 5 | 118 |
## | 0.042 | 0.839 | 0.076 | 0.042 | 0.502 |
## | 0.455 | 0.527 | 0.474 | 0.294 | |
## | 0.021 | 0.421 | 0.038 | 0.021 | |
## -------------|-----------|-----------|-----------|-----------|-----------|
## Male | 6 | 89 | 10 | 12 | 117 |
## | 0.051 | 0.761 | 0.085 | 0.103 | 0.498 |
## | 0.545 | 0.473 | 0.526 | 0.706 | |
## | 0.026 | 0.379 | 0.043 | 0.051 | |
## -------------|-----------|-----------|-----------|-----------|-----------|
## Column Total | 11 | 188 | 19 | 17 | 235 |
## | 0.047 | 0.800 | 0.081 | 0.072 | |
## -------------|-----------|-----------|-----------|-----------|-----------|
##
##
## As per occurence: Count. Row proportion. Column proportion. Total proportion.