Skip to content

Commit

Permalink
much improved directory of visualizations, major clean-up and full re…
Browse files Browse the repository at this point in the history
…-render of everything
  • Loading branch information
clauswilke committed Oct 22, 2017
1 parent e2cfe14 commit e585dea
Show file tree
Hide file tree
Showing 57 changed files with 446 additions and 1,766 deletions.
2 changes: 1 addition & 1 deletion _output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bookdown::gitbook:
split_by: chapter
config:
toc:
collapse: subsection
collapse: section
before: |
<li><a href="./">Data Visualization</a></li>
after: |
Expand Down
234 changes: 144 additions & 90 deletions directory_of_visualizations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ source("_common.R")
library(dplyr)
library(ggforce)
library(ggjoy)
library(ggridges)
library(colorspace)
```

# (PART\*) Part II: A visualization for every occasion {-}

# Directory of visualizations

```{r fig.width = 8, fig.asp = 1/4, message = FALSE, warning=FALSE}
```{r message = FALSE, warning = FALSE}
## general setup code
# theme
theme_plot_icon <- function(bg_color = "#F5F8EA", line_color = "#243400") {
line_size <- .8
font_size <- 10
Expand All @@ -33,144 +37,193 @@ theme_plot_icon <- function(bg_color = "#F5F8EA", line_color = "#243400") {
)
}
# data sets
set.seed(5142)
n <- 15
x <- rnorm(n)
y <- .4*x + .6*rnorm(n)
df_scatter_xy <- data.frame(x, y)
df_one_dist <- data.frame(x = c(rnorm(1000, 1., 1.6), rnorm(350, 4, .4)))
df_one_normal <- data.frame(x = rnorm(20))
df_fractions <- data.frame(y = c(.3, .39, .48, .6, .25, .13, .22, .24, .45, .48, .3, .16),
x = factor(rep(1:4, 3)),
type = rep(c("A", "B", "C"), each = 4))
n <- 70
df_multi_dist <- data.frame(y = c(rnorm(n, 1, .8), rnorm(n, 2, .7), rnorm(n, 0, .5)),
type = rep(c("A", "B", "C"), each = n),
number = rep(c(2, 1, 3), each = n))
# palettes
npal <- 5
# earth-brown
# HCL Wizard settings: 71, 80, 10, 18, 97, 1.5, 2
palette1 = c("#402B00", "#7F6B00", "#B6A87C", "#E2DAC4", "#FBF6EA")
pal_earth_brown <- sequential_hcl(n = npal, h1 = 71, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# brown-green
# HCL Wizard settings: 86, 80, 10, 18, 97, 1.5, 2
palette2 = c("#313100", "#707000", "#ABAB7B", "#DCDCC3", "#F7F7EA")
pal_brown_green <- sequential_hcl(n = npal, h1 = 86, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# green-brown
# HCL Wizard settings: -265, 80, 10, 18, 97, 1.5, 2
palette3 = c("#243400", "#667300", "#A4AD7C", "#D8DDC4", "#F5F8EA")
pal_green_brown <- sequential_hcl(n = npal, h1 = -265, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# burgundy-red
# HCL Wizard settings: 17, 80, 10, 18, 97, 1.5, 2
palette4 = c("#670000", "#A1574F", "#CF9C98", "#F1D4D1", "#FFF3F2")
pal_burgundy_red <- sequential_hcl(n = npal, h1 = 17, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# brown-red
# HCL Wizard settings: 41, 80, 10, 18, 97, 1.5, 2
palette5 = c("#581900", "#96602B", "#C7A188", "#ECD6C9", "#FFF4ED")
pal_brown_red <- sequential_hcl(n = npal, h1 = 41, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# ocean-blue
# HCL Wizard settings: 241, 80, 10, 18, 97, 1.5, 2
palette6 = c("#003F9C", "#2372A3", "#8DACCC", "#CDDCEE", "#EFF7FF")
pal_ocean_blue <- sequential_hcl(n = npal, h1 = 241, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# steel-blue
# HCL Wizard settings: 257, 80, 10, 18, 97, 1.5, 2
palette7 = c("#0021B1", "#546AA8", "#9DA7CF", "#D4DAF0", "#F3F6FF")
pal_steel_blue <- sequential_hcl(n = npal, h1 = 257, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
```

palette <- palette1
## Individual distributions

n <- 15
x <- rnorm(15)
y <- .4*x + .6*rnorm(15)
df <- data.frame(x, y)
p1 <- ggplot(df, aes(x, y)) +
geom_point(color = palette[3], size = 2.4) +
scale_x_continuous(expand = c(.2, 0)) +
scale_y_continuous(expand = c(.2, 0)) +
labs(title = "Scatterplot") +
theme_plot_icon(palette1[length(palette)], palette[1])
```{r single-distributions, fig.width = 8, fig.asp = 1/4, message = FALSE, warning=FALSE}
df <- data.frame(x = c(rnorm(1000, 1., 1.6), rnorm(350, 4, .4)))
p2 <- ggplot(df, aes(x)) +
geom_density(fill = palette[4], color = palette[2], bw = .35) +
scale_x_continuous(limits = c(-4.8, 6.8), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, .29), expand = c(0, 0)) +
labs(title = "Density plot") +
theme_plot_icon(palette[length(palette)], palette[1])
palette <- pal_earth_brown
p3 <- ggplot(df, aes(x)) +
p1 <- ggplot(df_one_dist, aes(x)) +
geom_histogram(fill = palette[4], color = palette[2], bins = 18) +
scale_x_continuous(limits = c(-4.8, 6.8), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 290),
expand = c(0, 0)) +
labs(title = "Histogram") +
theme_plot_icon(palette[length(palette)], palette[1])
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p2, p3, ncol = 4, scale = .9)
p2 <- ggplot(df_one_dist, aes(x)) +
geom_density(fill = palette[4], color = palette[2], bw = .35) +
scale_x_continuous(limits = c(-4.8, 6.8), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, .29), expand = c(0, 0)) +
labs(title = "Density plot") +
theme_plot_icon(palette[npal], palette[1])
p3 <- ggplot(df_one_normal, aes(x)) +
stat_ecdf(color = palette[2], size = .7) +
scale_x_continuous(expand = c(0.05, 0)) +
scale_y_continuous(limits = c(0, 1.1), expand = c(0, 0)) +
labs(title = "Cumulative density") +
theme_plot_icon(palette[npal], palette[1])
p4 <- ggplot(df_one_normal, aes(sample = x)) +
geom_abline(intercept = 0, slope = 1, color = palette[4]) +
geom_qq(color = palette[2]) +
labs(title = "q-q plot") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p2, p3, p4, ncol = 4, scale = .9)
```

```{r fig.width = 8, fig.asp = 1/4, message = FALSE}
palette <- palette2
Histograms and density plots (Chapter \@ref(histograms-density-plots)) provide the most intuitive visualizations of a distribution, but both require arbitrary parameter choices and can be misleading. Cumulative densities and q-q plots (Chapter \@ref(ecdf-qq)) always represent the data faithfully but can be more difficult to interpret.

df <- data.frame(y = c(.3, .39, .48, .6, .25, .13, .22, .24, .45, .48, .3, .16),
x = factor(rep(1:4, 3)),
type = rep(c("A", "B", "C"), each = 4))
p1 <- ggplot(filter(df, x!=4), aes(x, y,
fill=factor(type, levels = c("A", "C", "B")))) +
geom_col(position="dodge", color = palette[1],
width = .7) +
scale_y_continuous(expand = c(0, 0),
limits = c(0, .58)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Bar plot") +
theme_plot_icon(palette[length(palette)], palette[1])
## Multiple distributions

p2 <- ggplot(df, aes(x, y, fill=type)) +
geom_col(position="stack", color = palette[1]) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked bar plot") +
theme_plot_icon(palette[length(palette)], palette[1])
```{r multiple-distributions, fig.width = 8, fig.asp = 1/2, message = FALSE}
palette <- pal_ocean_blue
plot_grid(p1, p2, ncol = 4, scale = .9)
```
p1 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_boxplot(color = palette[1], fill = palette[4]) +
labs(title = "Boxplots") +
theme_plot_icon(palette[npal], palette[1])
```{r fig.width = 8, fig.asp = 1/2, message = FALSE}
palette <- palette6
n <- 70
df <- data.frame(y = c(rnorm(n, 1, .8), rnorm(n, 2, .7), rnorm(n, 0, .5)),
type = rep(c("A", "B", "C"), each = n),
number = rep(c(2, 1, 3), each = n))
p1 <- ggplot(df, aes(x = type, y = y)) + geom_boxplot(color = palette[1], fill = palette[4]) +
labs(title = "Boxplot") +
theme_plot_icon(palette[length(palette)], palette[1])
p2 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_violin(color = palette[1], fill = palette[4]) +
labs(title = "Violin plots") +
theme_plot_icon(palette[npal], palette[1])
p2 <- ggplot(df, aes(x = type, y = y)) + geom_violin(color = palette[1], fill = palette[4]) +
labs(title = "Violin plot") +
theme_plot_icon(palette[length(palette)], palette[1])
p3 <- ggplot(df, aes(x = type, y = y)) + geom_jitter(color = palette[1], width = 0.3, size = .8) +
p3 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_jitter(color = palette[1], width = 0.3, size = .8) +
labs(title = "Jittered points") +
theme_plot_icon(palette[length(palette)], palette[1])
theme_plot_icon(palette[npal], palette[1])
p4 <- ggplot(df, aes(x = type, y = y)) + geom_sina(color = palette[1], size = 0.8) +
p4 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_sina(color = palette[1], size = 0.8) +
labs(title = "Sina plot") +
theme_plot_icon(palette[length(palette)], palette[1])
theme_plot_icon(palette[npal], palette[1])
p5 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_histogram(color = palette[1], bins = 12) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked histograms") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 49), expand = c(0, 0)) +
theme_plot_icon(palette[npal], palette[1])
p5 <- ggplot(df, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
p6 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_density(alpha = 0.7, color = palette[1]) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Overlapping densities plot") +
labs(title = "Overlapping densities") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, .95), expand = c(0, 0)) +
theme_plot_icon(palette[length(palette)], palette[1])
theme_plot_icon(palette[npal], palette[1])
p6 <- ggplot(df, aes(x = y, y = number, group = number)) +
geom_joy(alpha = 0.7, fill = palette[3], scale = 2.0) +
labs(title = "Joyplot") +
p7 <- ggplot(df_multi_dist, aes(x = y, y = number, group = number)) +
geom_density_ridges(alpha = 0.7, color = palette[1], fill = palette[3], scale = 2.0) +
labs(title = "Ridgeline plot") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(1, 6.5), expand = c(0, 0)) +
theme_plot_icon(palette[length(palette)], palette[1])
theme_plot_icon(palette[npal], palette[1])
p7 <- ggplot(df, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
plot_grid(p1, p2, p3, p4,
p5, p6, p7, ncol = 4, scale = .9)
```


## Proportions

```{r proportions, fig.width = 8, fig.asp = 1/4, message = FALSE}
palette <- pal_brown_green
p1 <- ggplot(filter(df_fractions, x!=4), aes(x, y,
fill=factor(type, levels = c("A", "C", "B")))) +
geom_col(position="dodge", color = palette[1],
width = .7) +
scale_y_continuous(expand = c(0, 0),
limits = c(0, .58)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Bar plot") +
theme_plot_icon(palette[npal], palette[1])
p2 <- ggplot(df_fractions, aes(x, y, fill=type)) +
geom_col(position="stack", color = palette[1]) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked bar plot") +
theme_plot_icon(palette[npal], palette[1])
p3 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_density(alpha = 0.7, color = palette[1], position = "fill") +
scale_fill_manual(values = palette[2:4]) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
labs(title = "Stacked densities plot") +
theme_plot_icon(palette[length(palette)], palette[1])
labs(title = "Stacked densities") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p2, p3, p4,
p5, p6, p7, ncol = 4, scale = .9)
plot_grid(p1, p2, p3, ncol = 4, scale = .9)
```

## Other

```{r fig.width = 8, fig.asp = 1/4, message = FALSE, warning=FALSE}
palette <- pal_steel_blue
p1 <- ggplot(df_scatter_xy, aes(x, y)) +
geom_point(color = palette[3], size = 2.4) +
scale_x_continuous(expand = c(.2, 0)) +
scale_y_continuous(expand = c(.2, 0)) +
labs(title = "Scatterplot") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, ncol = 4, scale = .9)
## plots to add:
Expand All @@ -187,3 +240,4 @@ plot_grid(p1, p2, p3, p4,
# - mosaic plot/stacked bars flipped to horizontal
# - pie chart
```

17 changes: 11 additions & 6 deletions docs/avoid-line-drawings.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,20 @@
<li class="chapter" data-level="8.4" data-path="overlapping-points.html"><a href="overlapping-points.html#d-histograms"><i class="fa fa-check"></i><b>8.4</b> 2d histograms</a></li>
</ul></li>
<li class="part"><span><b>Part II: A visualization for every occasion</b></span></li>
<li class="chapter" data-level="9" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html"><i class="fa fa-check"></i><b>9</b> Directory of visualizations</a></li>
<li class="chapter" data-level="9" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html"><i class="fa fa-check"></i><b>9</b> Directory of visualizations</a><ul>
<li class="chapter" data-level="9.1" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html#individual-distributions"><i class="fa fa-check"></i><b>9.1</b> Individual distributions</a></li>
<li class="chapter" data-level="9.2" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html#multiple-distributions"><i class="fa fa-check"></i><b>9.2</b> Multiple distributions</a></li>
<li class="chapter" data-level="9.3" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html#proportions"><i class="fa fa-check"></i><b>9.3</b> Proportions</a></li>
<li class="chapter" data-level="9.4" data-path="directory-of-visualizations.html"><a href="directory-of-visualizations.html#other"><i class="fa fa-check"></i><b>9.4</b> Other</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="histograms-density-plots.html"><a href="histograms-density-plots.html"><i class="fa fa-check"></i><b>10</b> Visualizing distributions: Histograms and density plots</a><ul>
<li class="chapter" data-level="10.1" data-path="histograms-density-plots.html"><a href="histograms-density-plots.html#visualizing-a-single-distribution"><i class="fa fa-check"></i><b>10.1</b> Visualizing a single distribution</a></li>
<li class="chapter" data-level="10.2" data-path="histograms-density-plots.html"><a href="histograms-density-plots.html#visualizing-multiple-distributions-at-the-same-time"><i class="fa fa-check"></i><b>10.2</b> Visualizing multiple distributions at the same time</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html"><a href="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html"><i class="fa fa-check"></i><b>11</b> Visualizing distributions: Empirical cumulative density functions and q-q plots</a><ul>
<li class="chapter" data-level="11.1" data-path="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html"><a href="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html#empirical-cumulative-density-functions"><i class="fa fa-check"></i><b>11.1</b> Empirical cumulative density functions</a></li>
<li class="chapter" data-level="11.2" data-path="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html"><a href="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html#highly-skewed-distributions"><i class="fa fa-check"></i><b>11.2</b> Highly skewed distributions</a></li>
<li class="chapter" data-level="11.3" data-path="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html"><a href="visualizing-distributions-empirical-cumulative-density-functions-and-q-q-plots.html#quantilequantile-plots"><i class="fa fa-check"></i><b>11.3</b> Quantile–quantile plots</a></li>
<li class="chapter" data-level="11" data-path="ecdf-qq.html"><a href="ecdf-qq.html"><i class="fa fa-check"></i><b>11</b> Visualizing distributions: Empirical cumulative density functions and q-q plots</a><ul>
<li class="chapter" data-level="11.1" data-path="ecdf-qq.html"><a href="ecdf-qq.html#empirical-cumulative-density-functions"><i class="fa fa-check"></i><b>11.1</b> Empirical cumulative density functions</a></li>
<li class="chapter" data-level="11.2" data-path="ecdf-qq.html"><a href="ecdf-qq.html#skewed-distributions"><i class="fa fa-check"></i><b>11.2</b> Highly skewed distributions</a></li>
<li class="chapter" data-level="11.3" data-path="ecdf-qq.html"><a href="ecdf-qq.html#qq-plots"><i class="fa fa-check"></i><b>11.3</b> Quantile–quantile plots</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="boxplots-violins.html"><a href="boxplots-violins.html"><i class="fa fa-check"></i><b>12</b> Boxplots, violin plots, and more</a></li>
<li class="part"><span><b>Part III: Miscellaneous topics</b></span></li>
Expand Down Expand Up @@ -181,7 +186,7 @@ <h1><span class="header-section-number">6</span> Avoid line drawings</h1>
},
"download": null,
"toc": {
"collapse": "subsection"
"collapse": "section"
}
});
});
Expand Down
Loading

0 comments on commit e585dea

Please sign in to comment.