Skip to content

Commit 346419e

Browse files
committed
rework entire text for latest cowplot and dviz.supp packages.
1 parent c51e4fa commit 346419e

File tree

113 files changed

+1755
-806
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1755
-806
lines changed

_bookdown.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
new_session: yes
2+
output_dir: "docs"
3+
delete_merged_file: false
24

35
rmd_files: [
46
"index.Rmd",
@@ -10,15 +12,15 @@ rmd_files: [
1012
"colorblind.Rmd",
1113
"redundant_coding.Rmd",
1214
"small_axis_labels.Rmd",
13-
#"aspect_ratio.Rmd",
15+
##"aspect_ratio.Rmd",
1416
"background_grids.Rmd",
1517
"avoid_line_drawings.Rmd",
16-
"proportional_ink.Rmd",
18+
##"proportional_ink.Rmd",
1719
"overlapping_points.Rmd",
1820

1921
# Part II
2022
"directory_of_visualizations.Rmd",
21-
#"visualizing_amounts.Rmd",
23+
##"visualizing_amounts.Rmd",
2224
"visualizing_proportions.Rmd",
2325
"visualizing_distributions_I.Rmd",
2426
"visualizing_distributions_II.Rmd",
@@ -31,5 +33,3 @@ rmd_files: [
3133

3234
"references.Rmd"
3335
]
34-
35-
output_dir: "docs"

_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"

_common.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ options(digits = 3)
33

44
knitr::opts_chunk$set(
55
echo = FALSE,
6+
message = FALSE,
7+
warnings = FALSE,
68
cache = FALSE,
79
fig.align = 'center',
810
fig.width = 6,
@@ -12,7 +14,4 @@ knitr::opts_chunk$set(
1214

1315
options(dplyr.print_min = 6, dplyr.print_max = 6)
1416

15-
library(cowplot)
16-
17-
source("R/misc.R")
18-
source("R/themes.R")
17+
library(dviz.supp)

avoid_line_drawings.Rmd

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ h3_bad <- ggplot(age_hist_3, aes(x = age, y = count)) + geom_col(width = 2., fil
2929
color = "black") +
3030
scale_y_continuous(limits = c(0, 86), expand = c(0, 0), breaks = 25*(0:5)) +
3131
scale_x_continuous(limits = c(0, 75), expand = c(0, 0)) +
32-
theme_dviz() +
32+
theme_half_open() +
3333
background_grid(major = "y", minor = "none")
3434
3535
stamp_bad(h3_bad)
@@ -41,7 +41,7 @@ stamp_bad(h3_bad)
4141
h3_good <- ggplot(age_hist_3, aes(x = age, y = count)) + geom_col(width = 2.7, fill = "#56B4E9") +
4242
scale_y_continuous(limits = c(0, 86), expand = c(0, 0), breaks = 25*(0:5)) +
4343
scale_x_continuous(limits = c(0, 75), expand = c(0, 0)) +
44-
theme_dviz() +
44+
theme_half_open() +
4545
background_grid(major = "y", minor = "none")
4646
4747
stamp_good(h3_good)
@@ -70,13 +70,14 @@ iris_lines <- ggplot(iris_dens, aes(x = Sepal.Length, y = density, linetype = Sp
7070
geom_density_line(stat = "identity", alpha = 0., size = 0.75) +
7171
geom_text(data = iris_max, aes(label = Species, hjust = hjust, vjust = vjust,
7272
x = Sepal.Length + nudge_x,
73-
y = density + nudge_y), size = 12/.pt) +
73+
y = density + nudge_y),
74+
inherit.aes = FALSE, size = 12/.pt) +
7475
scale_linetype_manual(values = c(1, 3, 4),
7576
breaks = c("virginica", "versicolor", "setosa"),
7677
guide = "none") +
7778
scale_x_continuous(expand = c(0, 0), name = "sepal length") +
7879
scale_y_continuous(limits = c(0, 1.5), expand = c(0, 0)) +
79-
theme_dviz()
80+
theme_half_open()
8081
8182
stamp_bad(iris_lines)
8283
```
@@ -90,13 +91,14 @@ iris_colored_lines <- ggplot(iris_dens, aes(x = Sepal.Length, y = density, color
9091
geom_density_line(stat = "identity", alpha = 0., size = 1) +
9192
geom_text(data = iris_max, aes(label = Species, hjust = hjust, vjust = vjust,
9293
x = Sepal.Length + nudge_x,
93-
y = density + nudge_y), size = 12/.pt) +
94+
y = density + nudge_y),
95+
inherit.aes = FALSE, size = 12/.pt) +
9496
scale_color_manual(values = c("#56B4E9", "#E69F00", "#009E73"),
9597
breaks = c("virginica", "versicolor", "setosa"),
9698
guide = "none") +
9799
scale_x_continuous(expand = c(0, 0), name = "sepal length") +
98100
scale_y_continuous(limits = c(0, 1.5), expand = c(0, 0)) +
99-
theme_dviz()
101+
theme_half_open()
100102
101103
stamp_ugly(iris_colored_lines)
102104
```
@@ -109,13 +111,14 @@ iris_filled <- ggplot(iris_dens, aes(x = Sepal.Length, y = density, fill = Speci
109111
geom_density_line(stat = "identity", alpha = 0.7) +
110112
geom_text(data = iris_max, aes(label = Species, hjust = hjust, vjust = vjust,
111113
x = Sepal.Length + nudge_x,
112-
y = density + nudge_y), size = 12/.pt) +
114+
y = density + nudge_y),
115+
inherit.aes = FALSE, size = 12/.pt) +
113116
scale_fill_manual(values = c("#56B4E9", "#E69F00", "#009E73"),
114117
breaks = c("virginica", "versicolor", "setosa"),
115118
guide = "none") +
116119
scale_x_continuous(expand = c(0, 0), name = "sepal length") +
117120
scale_y_continuous(limits = c(0, 1.5), expand = c(0, 0)) +
118-
theme_dviz()
121+
theme_half_open()
119122
120123
stamp_good(iris_filled)
121124
```

background_grids.Rmd

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ We could try to remove the grid altogether, but I think that is a worse option (
6565
(ref:price-plot-no-grid) Indexed stock price over time for four major tech companies.
6666

6767
```{r price-plot-no-grid, warning=FALSE, fig.cap='(ref:price-plot-no-grid)'}
68-
stamp_bad(price_plot + theme_dviz_half_open())
68+
stamp_bad(price_plot + theme_half_open())
6969
```
7070

7171
(ref:price-plot-refline) Indexed stock price over time for four major tech companies.
@@ -84,9 +84,7 @@ price_plot2 <- ggplot(prices, aes(x=date, y=price_indexed, color=ticker)) +
8484
scale_y_continuous(name="stock price, indexed",
8585
limits = c(0, 560),
8686
expand=c(0,0)) +
87-
theme_dviz_half_open()
88-
89-
87+
theme_half_open()
9088
9189
stamp_good(price_plot2)
9290
```
@@ -96,7 +94,7 @@ Alternatively, we can use just a minimal grid. In particular, for a plot where w
9694
(ref:price-plot-hgrid) Indexed stock price over time for four major tech companies.
9795

9896
```{r price-plot-hgrid, warning=FALSE, fig.cap='(ref:price-plot-hgrid)'}
99-
stamp_good(price_plot + theme_dviz_hgrid())
97+
stamp_good(price_plot + theme_minimal_hgrid())
10098
```
10199

102100
For such a minimal grid, we generally draw the lines orthogonally to direction along which the numbers of interest vary. Therefore, if instead of plotting the stock price over time we plot the five-year increase, as horizontal bars, then we will want to use vertical lines instead (Figure \@ref(fig:price-increase)).
@@ -122,7 +120,7 @@ perc_plot <- ggplot(perc_increase, aes(x=ticker, y=perc)) +
122120
breaks=c("FB", "GOOG", "MSFT", "AAPL"),
123121
labels=c("Facebook", "Alphabet", "Microsoft", "Apple")) +
124122
coord_flip() +
125-
theme_dviz_vgrid() +
123+
theme_minimal_vgrid() +
126124
theme(axis.title.y=element_blank()) # remove the unnecessary space generated by an empty label
127125
128126
stamp_good(perc_plot)
@@ -150,7 +148,7 @@ iris %>%
150148
scale_x_continuous(limits=c(1.95, 4.6), expand=c(0, 0)) +
151149
scale_y_continuous(limits=c(3.95, 8.1), expand=c(0, 0)) -> iris_plot
152150
153-
stamp_good(plot_grid(NULL, iris_plot + theme_dviz_grid(), NULL, nrow=1, rel_widths=c(0.02, 1, 0.04)))
151+
stamp_good(plot_grid(NULL, iris_plot + theme_minimal_grid(), NULL, nrow=1, rel_widths=c(0.02, 1, 0.04)))
154152
```
155153

156154
For figures where the relevant comparison is the *x* = *y* line, I prefer to draw a diagonal line rather than a grid. For example, consider Figure \@ref(fig:echave-et-al), which compares two sets of correlations for 209 protein structures. By drawing the diagonal line, we can see immediately which correlations are systematically stronger. The same observation is much harder to make when the figure has a background grid instead (Figure \@ref(fig:echave-et-al-grid)). Thus, even though Figure \@ref(fig:echave-et-al-grid) looks pleasing, I label it as bad.
@@ -167,7 +165,8 @@ nrg_plot <- ggplot(cor_table, aes(y=cor.RSA, x=-cor.WCN)) +
167165
coord_fixed() +
168166
xlab("rate–WCN correlation") +
169167
ylab("rate–RSA correlation") +
170-
labs(title = "") # add an empty title as a simple trick to move the plot downwards
168+
labs(title = "") +
169+
theme_half_open() # add an empty title as a simple trick to move the plot downwards
171170
172171
stamp_good(plot_grid(nrg_plot, NULL, nrow=1, rel_widths=c(1, 0.2)))
173172
```
@@ -184,8 +183,9 @@ nrg_plot2 <- ggplot(cor_table, aes(y=cor.RSA, x=-cor.WCN)) +
184183
coord_fixed() +
185184
xlab("rate–WCN correlation") +
186185
ylab("rate–RSA correlation") +
187-
background_grid(size.major=0.5) +
188186
labs(title = "") # add an empty title as a simple trick to move the plot downwards
187+
theme_half_open() +
188+
background_grid(size.major=0.5)
189189
190190
stamp_bad(plot_grid(nrg_plot2, NULL, nrow=1, rel_widths=c(1, 0.2)))
191191
```

boxplots_violins.Rmd

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ library(dplyr)
66
library(forcats)
77
library(ggridges)
88
library(ggforce)
9-
library(viridis)
109
```
1110

1211
# Boxplots, violin plots, and more {#boxplots-violins}
@@ -36,7 +35,8 @@ lincoln_errbar <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature
3635
stat_summary(fun.y = mean, fun.ymax = function(x) {mean(x) + 2*sd(x)},
3736
fun.ymin = function(x) {mean(x) - 2*sd(x)}, geom = "pointrange",
3837
fatten = 5) +
39-
xlab("Month")
38+
xlab("Month") +
39+
theme_half_open()
4040
4141
stamp_bad(lincoln_errbar)
4242
```
@@ -63,8 +63,8 @@ The line in the middle represents the median, and the box encloses the middle 50
6363
When we visualize the temperature dataset using boxplots, we obtain the following result.
6464
```{r boxplots, message=FALSE, warning=FALSE}
6565
lincoln_box <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
66-
geom_boxplot(fill = 'grey90') + xlab("Month")
67-
66+
geom_boxplot(fill = 'grey90') + xlab("Month") +
67+
theme_half_open()
6868
6969
stamp_good(lincoln_box)
7070
```
@@ -74,7 +74,8 @@ Using the boxplot visualization, we see clearly that temperature is highly skewe
7474

7575
```{r violin-plots, message=FALSE, warning=FALSE}
7676
lincoln_violin <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
77-
geom_violin(fill = 'grey90') + xlab("Month")
77+
geom_violin(fill = 'grey90') + xlab("Month") +
78+
theme_half_open()
7879
7980
stamp_good(lincoln_violin)
8081
```
@@ -83,15 +84,17 @@ stamp_good(lincoln_violin)
8384
We can also plot all individual points:
8485
```{r all-points-no-jitter, message=FALSE, warning=FALSE}
8586
lincoln_points <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
86-
geom_point() + xlab("Month")
87+
geom_point() + xlab("Month") +
88+
theme_half_open()
8789
8890
stamp_bad(lincoln_points)
8991
```
9092

9193

9294
```{r jittered-points, message=FALSE, warning=FALSE}
9395
lincoln_jitter <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
94-
geom_jitter(width = .15) + xlab("Month")
96+
geom_jitter(width = .15) + xlab("Month") +
97+
theme_half_open()
9598
9699
stamp_good(lincoln_jitter)
97100
```
@@ -100,7 +103,10 @@ stamp_good(lincoln_jitter)
100103
Finally, we can combine the best of both worlds and spread the dots out in proportion to the number of points with a similar y coordinate. This methods yields the sina plot, which shows each individual dot while also visualizing the distributions.
101104
```{r sina-plots, message=FALSE, warning=FALSE}
102105
lincoln_sina <- ggplot(lincoln_df, aes(x = month_short, y = `Mean Temperature [F]`)) +
103-
geom_violin(color = "transparent", fill = "gray90") + stat_sina(maxwidth = 0.8) + xlab("Month")
106+
geom_violin(color = "transparent", fill = "gray90") +
107+
stat_sina(maxwidth = 0.8) +
108+
xlab("Month") +
109+
theme_half_open()
104110
105111
stamp_good(lincoln_sina)
106112
```

colorblind.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ You can see examples using these scales throughout this book, for example in the
107107

108108

109109

110-
```{r}
110+
```{r eval = FALSE, include = FALSE}
111111
## Notes, entered as R comment so as to not appear in the printed output
112112
#Some useful posts on color-blind friendly palettes:
113113
#

datasets/Australian_athletes/scatter_plot.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library(DAAG)
22
library(ggplot2)
33
library(ggridges)
4+
library(dplyr)
45

56
male_ais <- filter(ais, sex=="m") %>%
67
filter(sport %in% c("B_Ball", "Field", "Swim", "T_400m", "T_Sprnt", "W_Polo")) %>%
@@ -14,9 +15,9 @@ male_ais <- filter(ais, sex=="m") %>%
1415

1516
male_ais$sport <- factor(male_ais$sport, levels = c("field", "water polo", "basketball", "swim", "track"))
1617

17-
ggplot(male_ais, aes(x=ht, y=pcBfat, color=sport, fill=sport, shape=sport)) +
18-
geom_point(size = 2, alpha = 0.7) +
18+
ggplot(male_ais, aes(x=ht, y=pcBfat, color=sport, fill=sport, shape=sport)) +
19+
geom_point(size = 3) +
1920
scale_shape_manual(values = 21:25) +
2021
scale_color_OkabeIto(order=c(2, 1, 3, 4, 5), darken = 0.3) +
21-
scale_fill_OkabeIto(order=c(2, 1, 3, 4, 5), darken = 0.1) +
22+
scale_fill_OkabeIto(order=c(2, 1, 3, 4, 5), darken = 0.1, alpha = 0.5) +
2223
theme_minimal()

directory_of_visualizations.Rmd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ source("_common.R")
55
library(dplyr)
66
library(ggforce)
77
library(ggridges)
8-
library(colorspace)
98
```
109

1110
# (PART\*) Part II: A visualization for every occasion {-}
@@ -19,7 +18,7 @@ library(colorspace)
1918
theme_plot_icon <- function(bg_color = "#F5F8EA", line_color = "#243400") {
2019
line_size <- .8
2120
font_size <- 10
22-
theme_dviz_half_open() %+replace% theme(
21+
theme_half_open() %+replace% theme(
2322
axis.text.x = element_blank(),
2423
axis.text.y = element_blank(),
2524
axis.title.x = element_blank(),

0 commit comments

Comments
 (0)