Day 3

Data: build-in data BOD

head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5
str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
summary(ToothGrowth)
##       len        supp         dose      
##  Min.   : 4.20   OJ:30   Min.   :0.500  
##  1st Qu.:13.07   VC:30   1st Qu.:0.500  
##  Median :19.25           Median :1.000  
##  Mean   :18.81           Mean   :1.167  
##  3rd Qu.:25.27           3rd Qu.:2.000  
##  Max.   :33.90           Max.   :2.000

boxplot base method

plot(ToothGrowth$supp, ToothGrowth$len) #because ToothGrowth$supp is a factor

boxplot(len ~ supp, data = ToothGrowth) #alternative

combine 2 independent variable

boxplot(len ~ supp + dose, data = ToothGrowth) #Put interaction of two variables on x-axis

ggplot2 method

qplot(ToothGrowth$supp, ToothGrowth$len, geom = "boxplot")

qplot(supp, len, data = ToothGrowth, geom = "boxplot") #alternative

ggplot(ToothGrowth, aes(supp, len)) + geom_boxplot() #alternative

interaction of two variables on x-axis

qplot(interaction(ToothGrowth$supp, ToothGrowth$dose), ToothGrowth$len, geom = "boxplot")

qplot(interaction(supp, dose), len, data = ToothGrowth, geom = "boxplot") #alternative

ggplot(ToothGrowth, aes(interaction(supp, dose), len)) + geom_boxplot()

function curve plot base method

curve(x^3 - 5*x, from = -4, to = 4)

plot a user defined function curve

myfun <- function(xvar){
        1/(1 + exp(-xvar + 10))
}

curve(myfun(x), from = 0, to =20)

myfun <- function(xvar){
        1/(1 + exp(-xvar + 10))
}

curve(myfun(x), from = 0, to =20)
curve(1 - myfun(x), add = TRUE, col = "red") #add a curve

###ggplot2 method

myfun <- function(xvar){
        1/(1 + exp(-xvar + 10))
}

ggplot(data.frame(x = c(0,20)), aes(x=x)) + stat_function(fun = myfun, geom = "line")