
## @knitr Options, include=F, cache=F, message=F
opts_chunk$set(size='footnotesize', eval=T, cache=T, prompt=F, message=F, tidy=F)


## @knitr gettinstarted, echo=T, results='hide', cache=T, message=F, size='normalsize'
library(ggplot2)
library(languageR)
library(Hmisc)
data(lexdec)

# define some functions
se <- function(x)
{
  y <- x[!is.na(x)] # remove the missing values, if any
  sqrt(var(as.vector(y))/length(y))
}


## @knitr histogram1, echo=T, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
# Create a histogram of RTs in the lexdec dataset
ggplot(lexdec, aes(x=RT)) +
  geom_histogram()


## @knitr histogram2, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT)) +
  geom_histogram(binwidth=0.02)


## @knitr histogram3, echo=T, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',eval=FALSE
## ggplot(lexdec, aes(x=RT)) +
##   geom_histogram(binwidth=0.02)


## @knitr density1, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT)) +
  geom_density()


## @knitr density2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
## ggplot(lexdec, aes(x=RT)) +
##   geom_density()


## @knitr dh1, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT)) +
  geom_histogram(aes(y = ..density..)) + 
  geom_density()


## @knitr dh2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
## ggplot(lexdec, aes(x=RT)) +
##   geom_histogram(aes(y = ..density..)) +
##   geom_density()


## @knitr facet1, echo=F, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.8\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT)) +
  geom_density() +
  facet_wrap( ~ Subject,nrow=4)


## @knitr facet2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
## ggplot(lexdec, aes(x=RT)) +
##   geom_density() +
##   facet_wrap( ~ Subject,nrow=4)


## @knitr color2, echo=T, results='hide', cache=T, message=F, size='small',fig.width=6, out.width='\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT)) +
  geom_histogram(fill="green")


## @knitr color1, echo=T, results='hide', cache=T, message=F, size='small',fig.width=6, out.width='\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT,fill="green")) +
  geom_histogram() 


## @knitr nl1, echo=F, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.6\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=RT, color=NativeLanguage)) +
  geom_density()


## @knitr nl2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
## ggplot(lexdec, aes(x=RT, color=NativeLanguage)) +
##   geom_density()


## @knitr agr, echo=T, results='markup', cache=T, size='small'
agr = aggregate(RT ~ NativeLanguage, 
                lexdec, mean)
agr


## @knitr bar1, echo=F, results='hide', fig.width=4, out.width='.6\\textwidth',fig.align='center'
ggplot(agr, aes(x=NativeLanguage,y=RT)) +
  geom_bar(stat="identity")


## @knitr bar2, echo=T, results='hide', eval=FALSE
## ggplot(agr, aes(x=NativeLanguage,y=RT)) +
##   geom_bar(stat="identity")


## @knitr bar3, echo=F, results='hide', fig.width=6, out.width='.6\\textwidth',fig.align='center'
agr = aggregate(RT ~ NativeLanguage + Class, lexdec, mean)
ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
  geom_bar(stat="identity",position="dodge")


## @knitr bar4, echo=T, results='hide', cache=T, message=F, eval=FALSE
## ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
##   geom_bar(stat="identity",position="dodge")


## @knitr errbar0, echo=F, results='hide'
agr = aggregate(RT ~ NativeLanguage + 
        Class, lexdec, mean)
agr$SE = aggregate(RT ~ NativeLanguage  
           + Class, lexdec, se)$RT
agr$YMin = agr$RT - agr$SE
agr$YMax = agr$RT + agr$SE


## @knitr errbar2, echo=F, results='hide', out.width='.7\\textwidth', fig.width=4, fig.align='center'
ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
  geom_bar(stat="identity",position="dodge") +
  geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25)


## @knitr errbar1, eval=F
agr = aggregate(RT ~ NativeLanguage + 
        Class, lexdec, mean)
agr$SE = aggregate(RT ~ NativeLanguage  
           + Class, lexdec, se)$RT
agr$YMin = agr$RT - agr$SE
agr$YMax = agr$RT + agr$SE


## @knitr errbar3, results='hide', fig.width=6, out.width='.7\\textwidth',fig.align='center', eval=FALSE
## ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
##   geom_bar(stat="identity", position="dodge") +
##   geom_errorbar(aes(ymin=YMin, ymax=YMax),width=0.25)
## 


## @knitr errbar4, echo=T, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.5\\textwidth',fig.align='center'
dodge = position_dodge(.9)
ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
  geom_bar(stat="identity",position=dodge) +
  geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge)



## @knitr errbar5b, echo=T, results='hide',fig.width=6, out.width='.5\\textwidth',fig.align='center'
ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
  geom_bar(stat="identity",position=dodge) +
  geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
  coord_cartesian(ylim=c(6,6.6))


## @knitr errbar6, echo=F, results='hide', fig.width=6, out.width='.5\\textwidth',fig.align='center'
ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
  geom_point(position=dodge) +
  geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
  coord_cartesian(ylim=c(6.2,6.6))


## @knitr errbar7, echo=T, results='hide',fig.width=6, out.width='.5\\textwidth',fig.align='center',eval=FALSE
## ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
##   geom_point(position=dodge) +
##   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
##   coord_cartesian(ylim=c(6.2,6.6))


## @knitr errbar8, echo=F, results='hide'
library(bootstrap)
theta <- function(x,xdata,na.rm=T) {mean(xdata[x],na.rm=na.rm)}
ci.low <- function(x,na.rm=T) {
  mean(x,na.rm=na.rm) - quantile(bootstrap(1:length(x),1000,theta,x,na.rm=na.rm)$thetastar,.025,na.rm=na.rm)}
ci.high <- function(x,na.rm=T) {
  quantile(bootstrap(1:length(x),1000,theta,x,na.rm=na.rm)$thetastar,.975,na.rm=na.rm) - mean(x,na.rm=na.rm)}

agrr = aggregate(RT ~ NativeLanguage + Class + Subject, data=lexdec, mean)
agr = aggregate(RT ~ NativeLanguage + Class, data=agrr, mean)
agr$CIH = aggregate(RT ~ NativeLanguage + Class, data=agrr, FUN=ci.high)$RT
agr$CIL = aggregate(RT ~ NativeLanguage + Class, data=agrr, FUN=ci.low)$RT
agr$YMin = agr$RT - agr$CIL
agr$YMax = agr$RT + agr$CIH


## @knitr errbar9, echo=F, results='hide',fig.width=6, out.width='\\textwidth',fig.align='center'
ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
  geom_point(position=dodge) +
  geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
  scale_y_continuous(limits=c(6,6.75))


## @knitr errbar10, ,eval=F
agrr = aggregate(RT ~ NativeLanguage + 
         Class + Subject, lexdec, mean)
agr = aggregate(RT ~ NativeLanguage + 
         Class, agrr, mean)
agr$CIH = aggregate(RT ~ NativeLanguage + 
             Class, agrr, ci.high)$RT
agr$CIL = aggregate(RT ~ NativeLanguage + 
             Class, agrr, ci.low)$RT
agr$YMin = agr$RT - agr$CIL
agr$YMax = agr$RT + agr$CIH


## @knitr errbar11,  results='hide', fig.width=6, out.width='.9\\textwidth',fig.align='center',eval=FALSE
## ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
##     geom_point(position=dodge) +
##     geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,
##                   position=dodge) +
##     scale_y_continuous(limits=c(6,6.75))
## 
## 


## @knitr sp1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT)) +
  geom_point()


## @knitr sp2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT)) +
##   geom_point()


## @knitr jitter1, echo=F, results='hide',fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1))



## @knitr jitter2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
## 


## @knitr lowess11, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth', fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth()



## @knitr lowess12, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
##   stat_smooth()
## 


## @knitr linear1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm")


## @knitr linear2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
##   stat_smooth(method="lm")


## @knitr sp3, echo=F, results='hide',fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1))



## @knitr sp4, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
## 


## @knitr shape1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1))



## @knitr shape2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
## 


## @knitr shapec1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage, 
                   color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1))



## @knitr shapec2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage, color=NativeLanguage)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
## 


## @knitr sm1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm")



## @knitr sm2, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
##   stat_smooth(method="lm")
## 


## @knitr binfs1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=cut2(FamilySize,g=3))) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm")



## @knitr cut
table(cut(lexdec$FamilySize,breaks=3))
table(cut2(lexdec$FamilySize,g=3))


## @knitr binfs2, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=cut2(FamilySize,g=3))) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm")



## @knitr text1, results='hide', fig.width=8, fig.height=5, out.width='.75\\textwidth',fig.align='center'
agr = aggregate(RT ~ Word + Frequency, lexdec, mean)
ggplot(agr, aes(x=Frequency, y=RT)) +
  geom_point() +
  geom_text(aes(label=Word),vjust=1)



## @knitr app1, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm") +
  scale_x_continuous(name="Log-transformed word frequency")


## @knitr theme1, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm") +
  theme(axis.text.x=element_text(size=12,color="blue"))


## @knitr theme2, echo=F, results='hide',fig.width=5, fig.height=4.5,out.width='\\textwidth',fig.align='center'
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm") +
  theme(axis.text=element_text(size=12), axis.title=element_text(color="red"), legend.position="top")


## @knitr theme3, results='hide', eval=FALSE
## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
##   stat_smooth(method="lm") +
##   theme(axis.text=element_text(size=12), axis.title=
##           element_text(color="red"), legend.position="top")


## @knitr theme4, results='hide', fig.width=6,out.width='.45\\textwidth',fig.align='center'
theme_set(theme_bw())
ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
  geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
  stat_smooth(method="lm") +
  theme(axis.text=element_text(size=12), axis.title=
          element_text(color="red"))


