Skip to content

Commit

Permalink
Chapter 14, 15, and appendix
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Aug 1, 2018
1 parent 8e8e2e8 commit 0b339f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
8 changes: 4 additions & 4 deletions ch14.Rmd
Expand Up @@ -415,11 +415,11 @@ As of this writing, the patchwork package is not available on CRAN, so you need
```

Once you have installed patchwork, you can start stiching plots together (Figure \@ref(fig:FIG-OUTPUT-PATCHWORK)).
```{r FIG-OUTPUT-PATCHWORK, fig.show="hold", fig.cap="Combining two plots together using `patchwork()`", fig.height=4, fig.width=7}
```{r FIG-OUTPUT-PATCHWORK, fig.show="hold", fig.cap="Combining two plots together using `patchwork()`", fig.height=3.5, fig.width=7}
library(patchwork)

plot1 <- ggplot(PlantGrowth, aes(x = weight)) +
geom_histogram(bins = 20)
geom_histogram(bins = 12)

plot2 <- ggplot(PlantGrowth, aes(x = group, y = weight, group = group)) +
geom_boxplot()
Expand All @@ -431,9 +431,9 @@ plot1 + plot2
Patchwork also allows you to determine how you want to lay out the ggplots in relation to each other, by adding a `plot_layout()` call. You can use this call to determine the number of columns you want the pplots to be arranged in (Figure \@ref(fig:FIG-OUTPUT-PATCHWORK-COLS)), and the size of the plots (Figure \@ref(fig:FIG-OUTPUT-PATCHWORK-HEIGHT)):
```{r FIG-OUTPUT-PATCHWORK-COLS, fig.show="hold", fig.cap="Using `plot_layout()` to specify that the plots should be arranged in 2 columns", fig.height=7, fig.width=7}
```{r FIG-OUTPUT-PATCHWORK-COLS, fig.show="hold", fig.cap="Using `plot_layout()` to specify that the plots should be arranged in 2 columns", fig.height=7, fig.width=8}
plot3 <- ggplot(PlantGrowth, aes(x = weight, fill = group)) +
geom_histogram(bins = 20, position = "dodge")
geom_density(alpha = 0.25)
plot1 + plot2 + plot3 +
plot_layout(ncol = 2)
Expand Down
24 changes: 11 additions & 13 deletions ch15.Rmd
Expand Up @@ -11,17 +11,12 @@ editor_options:
# persist across cached builds.
source("utils.R", local = TRUE)
knitr::opts_chunk$set(fig.width=3.5, fig.height=3.5)
# Need to load MASS before dplyr because of conflict with select()
suppressPackageStartupMessages({
library(MASS)
library(dplyr)
})
# Print less for the examples in this chapter
options(knit_print_df_rows = 4)
knitr::opts_chunk$set(
fig.width = 3.5,
fig.height = 3.5,
# Print less for the examples in this chapter
print_df_rows = c(2, 2)
)
```

Getting Your Data into Shape {#CHAPTER-DATAPREP}
Expand Down Expand Up @@ -293,6 +288,7 @@ Renaming a column using base R is a bit more verbose. It uses the `names()` func
ToothGrowth2 <- ToothGrowth
names(ToothGrowth2) # Print the names of the columns
# Rename "len" to "length"
names(ToothGrowth2)[names(ToothGrowth2) == "len"] <- "length"
Expand Down Expand Up @@ -453,7 +449,7 @@ factor(sizes, levels = c("small", "medium", "large"))

The order can also be specified with `levels` when the factor is first created:

```{r echo=FALSE}
```{r eval=FALSE}
factor(c("small", "large", "large", "small", "medium"),
levels = c("small", "medium", "large"))
```
Expand Down Expand Up @@ -571,8 +567,10 @@ mappings
# Create a list of the arguments to pass to fct_recode
args <- c(list(sizes), mappings)
# Look at the structure of the list
str(args)
# Use do.call to call fct_recode with the arguments
do.call(fct_recode, args)
```
Expand Down Expand Up @@ -1198,7 +1196,7 @@ ggplot(c2b, aes(x = Date, fill = Cult, y = Weight)) +
geom_col(position = "dodge")
```

```{r, FIG-DATAPREP-SUMMARIZE-MISSING-COMBO, ref.label=c("FIG-DATAPREP-SUMMARIZE-MISSING-COMBO-1", "FIG-DATAPREP-SUMMARIZE-MISSING-COMBO-2"), fig.show="hold", fig.cap="Bar graph with a missing combination (left); With missing combination filled (right)", fig.width=4, fig.height=3}
```{r, FIG-DATAPREP-SUMMARIZE-MISSING-COMBO, ref.label=c("FIG-DATAPREP-SUMMARIZE-MISSING-COMBO-1", "FIG-DATAPREP-SUMMARIZE-MISSING-COMBO-2"), fig.show="hold", fig.cap="Bar graph with a missing combination (left); With missing combination filled (right)", fig.width=4, fig.height=3, warning=FALSE}
```

When we used `complete()`, it filled in the missing combinations with `NA`. It's possible to fill with a different value, with the `fill` parameter. See `?complete` for more information.
Expand Down
1 change: 0 additions & 1 deletion ggplot-appendix.Rmd
Expand Up @@ -11,7 +11,6 @@ editor_options:
# persist across cached builds.
source("utils.R", local = TRUE)
knitr::opts_chunk$set(fig.width=4, fig.height=3.5)
```

(APPENDIX) Appendix {-}
Expand Down
50 changes: 26 additions & 24 deletions utils.R
@@ -1,21 +1,27 @@
# options(warn = -1)

library(ggplot2)
library(dplyr)
suppressPackageStartupMessages({
# Need to load MASS before dplyr because of conflict with select()
library(MASS)
library(ggplot2)
library(dplyr)
})


options(width = 95)

knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
cache = TRUE,
fig.show = "hold",
# out.width = NULL,
fig.align = "center",
fig.width = 5,
fig.height = 4,
out.width = NULL,
print_df_rows = c(3, 3)
collapse = TRUE,
comment = "#>",
cache = TRUE,
fig.show = "hold",
# out.width = NULL,
fig.align = "center",
fig.width = 5,
fig.height = 4,
out.width = NULL,
print_df_rows = c(3, 3),
print_tbl_rows = 6
)

# Seems to be necessary for captions for multiple images in a single Figure.
Expand Down Expand Up @@ -72,7 +78,7 @@ knit_print_data.frame <- function(x, ..., rows = knitr::opts_current$get("print_
# Need to print the head and tail of data frame in one go, so that
# alignment is correct.
output <- capture.output(
print(x[rownums, ], ...)
print(x[rownums, , drop = FALSE], ...)
)

if (nrow(x) > sum(rows)) {
Expand Down Expand Up @@ -131,6 +137,10 @@ knit_print_table <- function(x, ...) {
invisible()
}

knit_print_tbl <- function(x, ..., maxrows = knitr::opts_current$get("print_tbl_rows")) {
print(x, ..., n = maxrows)
}

# Need this explicit registration step because of some change in R 3.5
register_s3_method("knitr", "knit_print", "data.frame", knit_print_data.frame)
register_s3_method("knitr", "knit_print", "matrix", knit_print_data.frame)
Expand All @@ -141,6 +151,9 @@ register_s3_method("knitr", "knit_print", "ts", knit_print_ts)

register_s3_method("knitr", "knit_print", "table", knit_print_table)

register_s3_method("knitr", "knit_print", "grouped_df", knit_print_tbl)
register_s3_method("knitr", "knit_print", "tbl_df", knit_print_tbl)
register_s3_method("knitr", "knit_print", "tbl", knit_print_tbl)


register_s3_method("knitr", "knit_print", "sf",
Expand All @@ -149,14 +162,3 @@ register_s3_method("knitr", "knit_print", "sf",
}
)

register_s3_method("knitr", "knit_print", "tbl_df",
function(x, ..., maxrows = 8) {
tibble:::print.tbl_df(x, ..., n = maxrows)
}
)

register_s3_method("knitr", "knit_print", "tbl",
function(x, ..., maxrows = 8) {
tibble:::print.tbl(x, ..., n = maxrows)
}
)

0 comments on commit 0b339f4

Please sign in to comment.