Attachment 'LSA13-Lecture5-ggplot.R'

Download

   1 ## @knitr Options, include=F, cache=F, message=F
   2 opts_chunk$set(size='footnotesize', eval=T, cache=T, prompt=F, message=F, tidy=F)
   3 
   4 
   5 ## @knitr gettinstarted, echo=T, results='hide', cache=T, message=F, size='normalsize'
   6 library(ggplot2)
   7 library(languageR)
   8 library(Hmisc)
   9 data(lexdec)
  10 
  11 # define some functions
  12 se <- function(x)
  13 {
  14   y <- x[!is.na(x)] # remove the missing values, if any
  15   sqrt(var(as.vector(y))/length(y))
  16 }
  17 
  18 
  19 ## @knitr histogram1, echo=T, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
  20 # Create a histogram of RTs in the lexdec dataset
  21 ggplot(lexdec, aes(x=RT)) +
  22   geom_histogram()
  23 
  24 
  25 ## @knitr histogram2, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
  26 ggplot(lexdec, aes(x=RT)) +
  27   geom_histogram(binwidth=0.02)
  28 
  29 
  30 ## @knitr histogram3, echo=T, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',eval=FALSE
  31 ## ggplot(lexdec, aes(x=RT)) +
  32 ##   geom_histogram(binwidth=0.02)
  33 
  34 
  35 ## @knitr density1, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
  36 ggplot(lexdec, aes(x=RT)) +
  37   geom_density()
  38 
  39 
  40 ## @knitr density2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
  41 ## ggplot(lexdec, aes(x=RT)) +
  42 ##   geom_density()
  43 
  44 
  45 ## @knitr dh1, echo=F, results='hide', cache=T, message=F, size='normalsize',out.width='.5\\textwidth',fig.align='center'
  46 ggplot(lexdec, aes(x=RT)) +
  47   geom_histogram(aes(y = ..density..)) + 
  48   geom_density()
  49 
  50 
  51 ## @knitr dh2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
  52 ## ggplot(lexdec, aes(x=RT)) +
  53 ##   geom_histogram(aes(y = ..density..)) +
  54 ##   geom_density()
  55 
  56 
  57 ## @knitr facet1, echo=F, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.8\\textwidth',fig.align='center'
  58 ggplot(lexdec, aes(x=RT)) +
  59   geom_density() +
  60   facet_wrap( ~ Subject,nrow=4)
  61 
  62 
  63 ## @knitr facet2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
  64 ## ggplot(lexdec, aes(x=RT)) +
  65 ##   geom_density() +
  66 ##   facet_wrap( ~ Subject,nrow=4)
  67 
  68 
  69 ## @knitr color2, echo=T, results='hide', cache=T, message=F, size='small',fig.width=6, out.width='\\textwidth',fig.align='center'
  70 ggplot(lexdec, aes(x=RT)) +
  71   geom_histogram(fill="green")
  72 
  73 
  74 ## @knitr color1, echo=T, results='hide', cache=T, message=F, size='small',fig.width=6, out.width='\\textwidth',fig.align='center'
  75 ggplot(lexdec, aes(x=RT,fill="green")) +
  76   geom_histogram() 
  77 
  78 
  79 ## @knitr nl1, echo=F, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.6\\textwidth',fig.align='center'
  80 ggplot(lexdec, aes(x=RT, color=NativeLanguage)) +
  81   geom_density()
  82 
  83 
  84 ## @knitr nl2, echo=T, results='hide', cache=T, message=F, size='normalsize',eval=FALSE
  85 ## ggplot(lexdec, aes(x=RT, color=NativeLanguage)) +
  86 ##   geom_density()
  87 
  88 
  89 ## @knitr agr, echo=T, results='markup', cache=T, size='small'
  90 agr = aggregate(RT ~ NativeLanguage, 
  91                 lexdec, mean)
  92 agr
  93 
  94 
  95 ## @knitr bar1, echo=F, results='hide', fig.width=4, out.width='.6\\textwidth',fig.align='center'
  96 ggplot(agr, aes(x=NativeLanguage,y=RT)) +
  97   geom_bar(stat="identity")
  98 
  99 
 100 ## @knitr bar2, echo=T, results='hide', eval=FALSE
 101 ## ggplot(agr, aes(x=NativeLanguage,y=RT)) +
 102 ##   geom_bar(stat="identity")
 103 
 104 
 105 ## @knitr bar3, echo=F, results='hide', fig.width=6, out.width='.6\\textwidth',fig.align='center'
 106 agr = aggregate(RT ~ NativeLanguage + Class, lexdec, mean)
 107 ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 108   geom_bar(stat="identity",position="dodge")
 109 
 110 
 111 ## @knitr bar4, echo=T, results='hide', cache=T, message=F, eval=FALSE
 112 ## ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 113 ##   geom_bar(stat="identity",position="dodge")
 114 
 115 
 116 ## @knitr errbar0, echo=F, results='hide'
 117 agr = aggregate(RT ~ NativeLanguage + 
 118         Class, lexdec, mean)
 119 agr$SE = aggregate(RT ~ NativeLanguage  
 120            + Class, lexdec, se)$RT
 121 agr$YMin = agr$RT - agr$SE
 122 agr$YMax = agr$RT + agr$SE
 123 
 124 
 125 ## @knitr errbar2, echo=F, results='hide', out.width='.7\\textwidth', fig.width=4, fig.align='center'
 126 ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 127   geom_bar(stat="identity",position="dodge") +
 128   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25)
 129 
 130 
 131 ## @knitr errbar1, eval=F
 132 agr = aggregate(RT ~ NativeLanguage + 
 133         Class, lexdec, mean)
 134 agr$SE = aggregate(RT ~ NativeLanguage  
 135            + Class, lexdec, se)$RT
 136 agr$YMin = agr$RT - agr$SE
 137 agr$YMax = agr$RT + agr$SE
 138 
 139 
 140 ## @knitr errbar3, results='hide', fig.width=6, out.width='.7\\textwidth',fig.align='center', eval=FALSE
 141 ## ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 142 ##   geom_bar(stat="identity", position="dodge") +
 143 ##   geom_errorbar(aes(ymin=YMin, ymax=YMax),width=0.25)
 144 ## 
 145 
 146 
 147 ## @knitr errbar4, echo=T, results='hide', cache=T, message=F, size='normalsize',fig.width=6, out.width='.5\\textwidth',fig.align='center'
 148 dodge = position_dodge(.9)
 149 ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 150   geom_bar(stat="identity",position=dodge) +
 151   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge)
 152 
 153 
 154 
 155 ## @knitr errbar5b, echo=T, results='hide',fig.width=6, out.width='.5\\textwidth',fig.align='center'
 156 ggplot(agr, aes(x=NativeLanguage, y=RT, fill=Class)) +
 157   geom_bar(stat="identity",position=dodge) +
 158   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
 159   coord_cartesian(ylim=c(6,6.6))
 160 
 161 
 162 ## @knitr errbar6, echo=F, results='hide', fig.width=6, out.width='.5\\textwidth',fig.align='center'
 163 ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
 164   geom_point(position=dodge) +
 165   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
 166   coord_cartesian(ylim=c(6.2,6.6))
 167 
 168 
 169 ## @knitr errbar7, echo=T, results='hide',fig.width=6, out.width='.5\\textwidth',fig.align='center',eval=FALSE
 170 ## ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
 171 ##   geom_point(position=dodge) +
 172 ##   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
 173 ##   coord_cartesian(ylim=c(6.2,6.6))
 174 
 175 
 176 ## @knitr errbar8, echo=F, results='hide'
 177 library(bootstrap)
 178 theta <- function(x,xdata,na.rm=T) {mean(xdata[x],na.rm=na.rm)}
 179 ci.low <- function(x,na.rm=T) {
 180   mean(x,na.rm=na.rm) - quantile(bootstrap(1:length(x),1000,theta,x,na.rm=na.rm)$thetastar,.025,na.rm=na.rm)}
 181 ci.high <- function(x,na.rm=T) {
 182   quantile(bootstrap(1:length(x),1000,theta,x,na.rm=na.rm)$thetastar,.975,na.rm=na.rm) - mean(x,na.rm=na.rm)}
 183 
 184 agrr = aggregate(RT ~ NativeLanguage + Class + Subject, data=lexdec, mean)
 185 agr = aggregate(RT ~ NativeLanguage + Class, data=agrr, mean)
 186 agr$CIH = aggregate(RT ~ NativeLanguage + Class, data=agrr, FUN=ci.high)$RT
 187 agr$CIL = aggregate(RT ~ NativeLanguage + Class, data=agrr, FUN=ci.low)$RT
 188 agr$YMin = agr$RT - agr$CIL
 189 agr$YMax = agr$RT + agr$CIH
 190 
 191 
 192 ## @knitr errbar9, echo=F, results='hide',fig.width=6, out.width='\\textwidth',fig.align='center'
 193 ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
 194   geom_point(position=dodge) +
 195   geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,position=dodge) +
 196   scale_y_continuous(limits=c(6,6.75))
 197 
 198 
 199 ## @knitr errbar10, ,eval=F
 200 agrr = aggregate(RT ~ NativeLanguage + 
 201          Class + Subject, lexdec, mean)
 202 agr = aggregate(RT ~ NativeLanguage + 
 203          Class, agrr, mean)
 204 agr$CIH = aggregate(RT ~ NativeLanguage + 
 205              Class, agrr, ci.high)$RT
 206 agr$CIL = aggregate(RT ~ NativeLanguage + 
 207              Class, agrr, ci.low)$RT
 208 agr$YMin = agr$RT - agr$CIL
 209 agr$YMax = agr$RT + agr$CIH
 210 
 211 
 212 ## @knitr errbar11,  results='hide', fig.width=6, out.width='.9\\textwidth',fig.align='center',eval=FALSE
 213 ## ggplot(agr, aes(x=NativeLanguage, y=RT, color=Class)) +
 214 ##     geom_point(position=dodge) +
 215 ##     geom_errorbar(aes(ymin=YMin,ymax=YMax),width=0.25,
 216 ##                   position=dodge) +
 217 ##     scale_y_continuous(limits=c(6,6.75))
 218 ## 
 219 ## 
 220 
 221 
 222 ## @knitr sp1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 223 ggplot(lexdec, aes(x=Frequency, y=RT)) +
 224   geom_point()
 225 
 226 
 227 ## @knitr sp2, results='hide', eval=FALSE
 228 ## ggplot(lexdec, aes(x=Frequency, y=RT)) +
 229 ##   geom_point()
 230 
 231 
 232 ## @knitr jitter1, echo=F, results='hide',fig.width=6, out.width='.45\\textwidth',fig.align='center'
 233 ggplot(lexdec, aes(x=Frequency, y=RT)) +
 234   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 235 
 236 
 237 
 238 ## @knitr jitter2, results='hide', eval=FALSE
 239 ## ggplot(lexdec, aes(x=Frequency, y=RT)) +
 240 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 241 ## 
 242 
 243 
 244 ## @knitr lowess11, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth', fig.align='center'
 245 ggplot(lexdec, aes(x=Frequency, y=RT)) +
 246   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 247   stat_smooth()
 248 
 249 
 250 
 251 ## @knitr lowess12, results='hide', eval=FALSE
 252 ## ggplot(lexdec, aes(x=Frequency, y=RT)) +
 253 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 254 ##   stat_smooth()
 255 ## 
 256 
 257 
 258 ## @knitr linear1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 259 ggplot(lexdec, aes(x=Frequency, y=RT)) +
 260   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 261   stat_smooth(method="lm")
 262 
 263 
 264 ## @knitr linear2, results='hide', eval=FALSE
 265 ## ggplot(lexdec, aes(x=Frequency, y=RT)) +
 266 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 267 ##   stat_smooth(method="lm")
 268 
 269 
 270 ## @knitr sp3, echo=F, results='hide',fig.width=6, out.width='.45\\textwidth',fig.align='center'
 271 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 272   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 273 
 274 
 275 
 276 ## @knitr sp4, results='hide', eval=FALSE
 277 ## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 278 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 279 ## 
 280 
 281 
 282 ## @knitr shape1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 283 ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage)) +
 284   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 285 
 286 
 287 
 288 ## @knitr shape2, results='hide', eval=FALSE
 289 ## ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage)) +
 290 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 291 ## 
 292 
 293 
 294 ## @knitr shapec1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 295 ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage, 
 296                    color=NativeLanguage)) +
 297   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 298 
 299 
 300 
 301 ## @knitr shapec2, results='hide', eval=FALSE
 302 ## ggplot(lexdec, aes(x=Frequency, y=RT, shape=NativeLanguage, color=NativeLanguage)) +
 303 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1))
 304 ## 
 305 
 306 
 307 ## @knitr sm1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 308 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 309   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 310   stat_smooth(method="lm")
 311 
 312 
 313 
 314 ## @knitr sm2, results='hide', eval=FALSE
 315 ## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 316 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 317 ##   stat_smooth(method="lm")
 318 ## 
 319 
 320 
 321 ## @knitr binfs1, echo=F, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 322 ggplot(lexdec, aes(x=Frequency, y=RT, color=cut2(FamilySize,g=3))) +
 323   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 324   stat_smooth(method="lm")
 325 
 326 
 327 
 328 ## @knitr cut
 329 table(cut(lexdec$FamilySize,breaks=3))
 330 table(cut2(lexdec$FamilySize,g=3))
 331 
 332 
 333 ## @knitr binfs2, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 334 ggplot(lexdec, aes(x=Frequency, y=RT, color=cut2(FamilySize,g=3))) +
 335   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 336   stat_smooth(method="lm")
 337 
 338 
 339 
 340 ## @knitr text1, results='hide', fig.width=8, fig.height=5, out.width='.75\\textwidth',fig.align='center'
 341 agr = aggregate(RT ~ Word + Frequency, lexdec, mean)
 342 ggplot(agr, aes(x=Frequency, y=RT)) +
 343   geom_point() +
 344   geom_text(aes(label=Word),vjust=1)
 345 
 346 
 347 
 348 ## @knitr app1, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 349 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 350   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 351   stat_smooth(method="lm") +
 352   scale_x_continuous(name="Log-transformed word frequency")
 353 
 354 
 355 ## @knitr theme1, results='hide', fig.width=6, out.width='.45\\textwidth',fig.align='center'
 356 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 357   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 358   stat_smooth(method="lm") +
 359   theme(axis.text.x=element_text(size=12,color="blue"))
 360 
 361 
 362 ## @knitr theme2, echo=F, results='hide',fig.width=5, fig.height=4.5,out.width='\\textwidth',fig.align='center'
 363 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 364   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 365   stat_smooth(method="lm") +
 366   theme(axis.text=element_text(size=12), axis.title=element_text(color="red"), legend.position="top")
 367 
 368 
 369 ## @knitr theme3, results='hide', eval=FALSE
 370 ## ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 371 ##   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 372 ##   stat_smooth(method="lm") +
 373 ##   theme(axis.text=element_text(size=12), axis.title=
 374 ##           element_text(color="red"), legend.position="top")
 375 
 376 
 377 ## @knitr theme4, results='hide', fig.width=6,out.width='.45\\textwidth',fig.align='center'
 378 theme_set(theme_bw())
 379 ggplot(lexdec, aes(x=Frequency, y=RT, color=NativeLanguage)) +
 380   geom_jitter(position = position_jitter(width = 0.1, height=0.1)) +
 381   stat_smooth(method="lm") +
 382   theme(axis.text=element_text(size=12), axis.title=
 383           element_text(color="red"))

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-04-22 12:55:37, 1045.4 KB) [[attachment:LSA13-AuxLecture 1&2 - Coding.pdf]]
  • [get | view] (2021-04-22 12:55:37, 10.7 KB) [[attachment:LSA13-AuxLecture5-TimeSeriesData-eye-tracking-analysis.R]]
  • [get | view] (2021-04-22 12:55:37, 378.0 KB) [[attachment:LSA13-AuxLecture5-TimeSeriesData.pdf]]
  • [get | view] (2021-04-22 12:55:37, 735.1 KB) [[attachment:LSA13-CourseOverview.pdf]]
  • [get | view] (2021-04-22 12:55:37, 8.6 KB) [[attachment:LSA13-Lecture1-GLM.R]]
  • [get | view] (2021-04-22 12:55:37, 2879.2 KB) [[attachment:LSA13-Lecture1-GLM.pdf]]
  • [get | view] (2021-04-22 12:55:37, 7.0 KB) [[attachment:LSA13-Lecture2-GLMM.R]]
  • [get | view] (2021-04-22 12:55:37, 1337.3 KB) [[attachment:LSA13-Lecture2-GLMM.pdf]]
  • [get | view] (2021-04-22 12:55:37, 1011.4 KB) [[attachment:LSA13-Lecture3-BeyondLinearModels.pdf]]
  • [get | view] (2021-04-22 12:55:37, 13.0 KB) [[attachment:LSA13-Lecture4-plyr-reshape.R]]
  • [get | view] (2021-04-22 12:55:37, 467.8 KB) [[attachment:LSA13-Lecture4-plyr-reshape.pdf]]
  • [get | view] (2021-04-22 12:55:37, 13.8 KB) [[attachment:LSA13-Lecture5-ggplot.R]]
  • [get | view] (2021-04-22 12:55:37, 2058.0 KB) [[attachment:LSA13-Lecture5-ggplot.pdf]]
  • [get | view] (2021-04-22 12:55:37, 2920.0 KB) [[attachment:LSA13-Lecture6-CommonIssuesAndSolutions.pdf]]
  • [get | view] (2021-04-22 12:55:37, 1581.0 KB) [[attachment:LSA13-Lecture6-Reporting.pdf]]
  • [get | view] (2021-04-22 12:55:37, 3.7 KB) [[attachment:LSA13-PreLecture2-ProblemSet.R]]
  • [get | view] (2021-04-22 12:55:37, 608.5 KB) [[attachment:LSA13-PreLecture2-ProblemSet.pdf]]
  • [get | view] (2021-04-22 12:55:37, 145.1 KB) [[attachment:data.zip]]
  • [get | view] (2021-04-22 12:55:37, 1284.6 KB) [[attachment:scripts.zip]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

MoinMoin Appliance - Powered by TurnKey Linux