Skip to content

Commit

Permalink
lattice log axis
Browse files Browse the repository at this point in the history
  • Loading branch information
sam81 committed Mar 29, 2017
1 parent bfc3d7a commit 2a6a28f
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 28 deletions.
6 changes: 4 additions & 2 deletions anova.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@


#ANOVA

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##One-Way ANOVA

We'll start directly with an example to show how to perform a one-way ANOVA. Let's imagine an experimenter is studying the effects of sleep deprivation on verbal memory. He selects a random sample of healthy subjects, and assigns them to three treatments: 4 hours, 6 hours and 8 hours of total sleep. The subjects are then given a memory test, consisting of a long list of words to remember after a minute of delay from their presentation. The dependent variable is the number of words correctly recalled in a minute time. The data are shown in table \@ref(tab:sleep):
Expand Down
4 changes: 4 additions & 0 deletions data.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#File Input/Output {#IO}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##Reading In Data from a File

###The `read.table` function {#read.table}
Expand Down
4 changes: 4 additions & 0 deletions data_manipulation.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Data Types and Data Manipulation {#data_manip}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##Vectors

One of the simplest and among the most important data types in R is the vector, which can be numerical or containing strings of characters. A simple way to build a vector is through the `c` function, which concatenates a series of data, for example:
Expand Down
4 changes: 4 additions & 0 deletions descriptives.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Descriptive

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##Tables

##The `scale` Function
Expand Down
4 changes: 4 additions & 0 deletions distributions.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Probability Distribution Functions

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##The Bernoulli distribution {#bernoulli}

A random variable $X$ that takes a value of 0 or 1 depending on the result of an experiment
Expand Down
4 changes: 4 additions & 0 deletions graphics.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Graphics

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

There are four main graphics libraries that can be used in R. The first is the base graphics system that comes builtin with every R installation and will be described in this chapter. The other three main graphics libraries (`ggplot2`, `lattice`, and `plotly`), can be installed as additional packages. Each of these libraries provide a complete, independent system to generate graphics in R. Choosing one library over the other is mostly a matter of personal preference because pretty much any graphic that can be built with one library can also be built with the others. Some graphics are easier to build with one library than another and vice-versa. In recent years `ggplot2` has gained lots of popularity and `lattice` is less popular than it was some years ago. Despite the increasing popularity of `ggplot2` the base R graphics that are described in this chapter are still widely used. `plotly` is a recent entry and is somehow a special case because it is primarily designed to generate interactive graphics that can be displayed on html pages, while the other graphics libraries have very limited interactive functionality and are primarily designed to generate static high-quality graphics. `plotly` is special also because through the `ggplotly` function it can convert a `ggplot2` graph into an interactive `plotly` graphic.

Because the base R graphics library is still widely used and (in my opinion) is somewhat simpler to use for beginners, I would recommend learning it first. The `ggplot2` library is described in chapter \@ref(ggplot2), the `lattice` graphics library is described in chapter \@ref(lattice), and the `plotly` library is described in chapter \@ref(plotly).
Expand Down
41 changes: 41 additions & 0 deletions graphics_ggplot2.Rmd
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
#ggplot2 {#ggplot2}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

The book "ggplot2. Elegant graphics for data analysis" [@Wickham2009] is the best
reference for learning ggplot2

Other useful resources include:

- AVML 2012: ggplot2 http://www.ling.upenn.edu/~joseff/avml2012/ by
Josef Fruehwald. This is one of the best introductions to ggplot2,
highly recommended! There is a more recent version of this tutorial
also here: http://jofrhwld.github.io/teaching/courses/2017_lvc/practicals/7_practical_r.html#inheritance

All the examples in this chapter assume that ggplot2 has been
installed and is loaded in your R session. If not, you can install it with:
```{r eval=FALSE}
install.packages("ggplot2")
```

and load it with:

```{r message=FALSE}
library(ggplot2)
```

###Log axis with pretty tickmarks

```{r fig.cap="log axis with pretty tickmarks"}
x = c("cnd1", "cnd2")
y = c(0.4, 80)
dat = data.frame(x=x, y=y)
p = ggplot(dat, aes(x=x, y=y)) + geom_point()
p = p + scale_y_continuous(trans="log10")
p = p + annotation_logticks(sides="l")
p = p + theme_bw(base_size=12)
p
```
105 changes: 92 additions & 13 deletions graphics_lattice.Rmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#Lattice or Trellis Graphics {#lattice}
#Lattice Graphics {#lattice}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

The `lattice` package in R provides an alternative to the base graphics system, it is an implementation of the ideas developed and implemented by Rick Becker and Bill Cleveland in the Trellis graphics system for the S language. Trellis displays were developed as a framework to make it easy the display of the relationship between a dependent variable and multiple factors.

Expand Down Expand Up @@ -180,18 +184,94 @@ print(bwplot(dats$lat~dats$congr|dats$acc,groups=dats$isi,

####Strip Labels

The labels of the panels strips are the names of the levels of the conditioning factor variable. To change the labels just change the names for the factor levels:
```{r eval=FALSE}
levels(dats$acc)
[1] "e" "h"
levels(dats$acc)[1]
[1] "e"
levels(dats$acc)[1]<-'imprecise'
levels(dats$acc)[2]<-'corrette'
levels(dats$acc)
[1] "imprecise" "corrette"
The labels of the panels strips are the names of the levels of the conditioning factor variable. To change their labels you can pass the `factor.levels` argument to the `strip.custom` function:

```{r strip1, fig.cap="Custom strip labels"}
x = rnorm(20)
y = rnorm(20)
treat = factor(rep(c("A", "B"), each=10))
xyplot(y~x|treat, strip=strip.custom(factor.levels=c("Group A", "Group B")))
```

Alternatively, you change the names for the factor levels:
```{r}
levels(treat) = c("Group A", "Group B")
```

if you have more than one conditioning factor you can customize the strip labels for each by passing a custom strip function. `which.given` specifies the conditioning
variable that the strip corresponds to:

```{r strip2, fig.cap="Custom strip labels with two conditioning factors"}
cnd = factor(rep(c("I", "II"), 10))
customstrip = function(which.given, ..., factor.levels){
levs = if (which.given==1){
c("Group A", "Group B")
} else if (which.given==2){
c("Cnd. I", "Cnd. II")
}
strip.default(which.given, ..., factor.levels = levs)
}
xyplot(y~x | treat + cnd, strip=customstrip, as.table=TRUE)
```


###Log axis with pretty tickmarks

The procedure to get a log axis with pretty tickmarks in lattice is a bit involved. We'll only cover the log base 10 case here. The first step is to define a function that returns the tick locations:

```{r}
log10Ticks = function(lim, onlyMajor=FALSE){
minPow = floor(log10(lim[1]))
maxPow = ceiling(log10(lim[2]))
powSeq = seq(minPow, maxPow)
majTicks = 10^powSeq
minTicks = numeric()
for (i in 1:length(majTicks)){
bb = (1:10)/10;
minTicks = c(minTicks, (bb*10^powSeq[i]))
}
if (onlyMajor==TRUE){
axSeq = majTicks
} else {
axSeq = minTicks
}
axSeq = axSeq[lim[1] <= axSeq & axSeq <= lim[2]]
return(axSeq)
}
```

by default the function returns both the major (e.g. 1, 10, 100, etc...) and the minor (e.g. 2,3,4,...20,30,40, etc...) tick locations, but return only the major tick locations if `onlyMajor=TRUE`.
This function will be used by the `yscale.components.log10` function below. This function will be passed as the `yscale.components` argument in the `xyplot` call that generates the graph. The `yscale.components.log10` function will return a list specifying all parameters of the y axis. To simplify this process the function calls the `yscale.components.default` function to retrieve the default parameters, and then simply modifies some of these parameters to draw the pretty log axis:

```{r}
yscale.components.log10 = function(lim, ...){ #the function is automatically passed the limits of the panel as an argument
ans = yscale.components.default(lim = lim, ...) #retrieve default parameters
tick.at = log10Ticks(10^lim, onlyMajor=FALSE) #compute major and minor tick locations
tick.at.major = log10Ticks(10^lim, onlyMajor=TRUE) #compute major tick locations only
major = tick.at %in% tick.at.major #which are the major ticks?
ans$left$ticks$at = log10(tick.at) #where the ticks should be position
ans$left$ticks$tck = ifelse(major, 1.5, 0.75) #set tick length, depending on whether minor or major
ans$left$labels$at = log10(tick.at) #labels location
ans$left$labels$labels = as.character(tick.at) #set tick labels
ans$left$labels$labels[!major] = "" #set minor tick labels as empty
ans$left$labels$check.overlap = FALSE
return(ans)
}
```

once the `yscale.components.log10` function is ready, we can use it in the call to `xyplot`, note that we also need to set the y scale to log10 in the `scales` argument:

```{r fig.cap="log axis with pretty tickmarks"}
x = c("cnd1", "cnd2")
y = c(0.4, 80)
dat = data.frame(x=x, y=y)
xyplot(y~x, data=dat,
scales=list(y=list(log=10)),
yscale.components = yscale.components.log10)
```




##Writing Panel Functions {#trellispanel}
Expand All @@ -213,7 +293,7 @@ dotplot(error ~ length | group, groups=color,
aspect=1, as.table=TRUE)
```

however, since the data represent positive or negative displacements from zero, it would be nice to add a horizontal line passing from zero. In order to have this, we will write a panel function that combines the `panel.dotplot` function with the `panel.abline` function that we'll use to add the horizontal line:
however, since the data represent positive or negative displacements from zero, it would be nice to add a horizontal line passing at zero. In order to have this, we will write a panel function that combines the `panel.dotplot` function with the `panel.abline` function that we'll use to add the horizontal line:

```{r}
panel.hRefDotplot <- function(x, y, ref=NULL, ...){
Expand All @@ -232,4 +312,3 @@ dotplot(error ~ length | group, groups=color,
```

notice the last line of the call, first we're telling `dotplot` to use our `hRefDotplot` function to do the plotting by specifying the `panel` argument, second we're specifying another argument, `ref` in the call, this is not a standard argument, but it will be automatically passed to our panel function to decide at which height to draw the horizontal line. The resulting plot can be visualised in Figure \@ref(fig:lmatchdotplotref).

15 changes: 15 additions & 0 deletions graphics_par.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

##Setting Graphics Parameters {#par}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

Graphics parameters allow you to tweak many elements of a plot, such as the font for the labels, the symbols or line types to use and so on, see `?par` to get a full list and description of these parameters. Graphics parameters can be set and accessed with the function `par`, called without arguments, as `par`, it will give you a full list of the current defaults, if you want to query only one or a few parameters use:
```{r}
par("lwd") ## see current line width
Expand Down Expand Up @@ -522,7 +526,18 @@ The function `col2rgb` can be used to get the rgb values of a built-in colour fr
col2rgb("lightslateblue")
```

### Colour Opacity

The `adjustcolor` function can be used to set the opacity of a color using the `alpha.f` argument:

```{r coltransp, fig.cap="Colour opacity"}
mycol = adjustcolor("skyblue", alpha.f=0.5)
x = rnorm(1000)
y = rnorm(1000)
plot(x,y, pch=21, bg=mycol, cex=2)
```

the `adjustcolor` function accepts a vector of colors as an argument, so you can change the transparency of several colours at


##Managing Graphic Devices
Expand Down
4 changes: 4 additions & 0 deletions graphics_plotly.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Plotly {#plotly}

The ["plotly for R"](https://cpsievert.github.io/plotly_book/) book [@Sievert2017] is the best introduction for
learning plotly.


```{r plotly1, message=FALSE, warning=FALSE, fig.cap="Plotly example"}
library(plotly)
x = rnorm(10); y=rnorm(10)
Expand Down
4 changes: 4 additions & 0 deletions hypotheses.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Hypothesis Testing

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##$\chi^2$ test

###Testing Hypotheses about the Distribution of a Categorical Variable
Expand Down
1 change: 0 additions & 1 deletion index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ always_allow_html: yes
description: "A guide for the R programming language."
---


4 changes: 4 additions & 0 deletions intro.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Installing R {#intro}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

The information provided in this section is quite generic and limited. For detailed information please refer to the "R Installation and Administration Manual" available at the CRAN website https://cran.r-project.org. CRAN stands for "Comprehensive R Archive Network", and is the main point of reference for the R software, where you can find R sources, binaries, documentation, and add-on packages.

## Installing the Base Program
Expand Down
4 changes: 4 additions & 0 deletions mcomp.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#How to Adjust the p-values for Multiple Comparisons

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

The base R program comes with a number of functions to perform multiple comparisons. Here by "multiple comparisons" I mean both post-hoc, and planned comparisons procedures. Not all the possible procedures are available, and some of them might not be applicable to the object resulting from the specific analysis you're doing. Additional packages might cover your specific needs.

####The `p.adjust` function
Expand Down
5 changes: 4 additions & 1 deletion programming.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#R Programming {#programming}

```{r setup, include=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=6)
```

##Control Structures {#contrstruct}

####If..Else Conditional Execution {#ifelse}
Expand Down
Loading

0 comments on commit 2a6a28f

Please sign in to comment.